在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);