在MySQL中,SUM函数可以用来计算某列的总和。而加上DISTINCT关键字可以确保只计算不重复的值的总和。以下是一些MySQL SUM DISTINCT的技巧:
SELECT SUM(DISTINCT column_name) FROM table_name;
SELECT column_name, SUM(DISTINCT column_name) FROM table_name GROUP BY column_name;
SELECT column_name, (SELECT SUM(DISTINCT column_name) FROM table_name WHERE condition) AS distinct_sum FROM table_name;
SELECT SUM(column_name) FROM table_name WHERE column_name IN (SELECT DISTINCT column_name FROM table_name);