在SQL中,可以使用以下方式進行去重:
使用DISTINCT關鍵字:SELECT DISTINCT column1, column2 FROM table_name;
使用GROUP BY子句:SELECT column1, column2 FROM table_name GROUP BY column1, column2;
使用HAVING子句:SELECT column1, column2 FROM table_name GROUP BY column1, column2 HAVING COUNT(*) > 1;
使用ROW_NUMBER()函數:WITH cte AS (SELECT column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY column1, column2) AS rn FROM table_name) SELECT column1, column2 FROM cte WHERE rn = 1;
使用UNION或UNION ALL操作符:SELECT column1, column2 FROM table1 UNION SELECT column1, column2 FROM table2;
使用EXISTS子查詢:SELECT column1, column2 FROM table_name t1 WHERE NOT EXISTS (SELECT 1 FROM table_name t2 WHERE t1.column1 = t2.column1 AND t1.column2 = t2.column2 AND t1.column3 = t2.column3);
使用TEMPORARY TABLE或CTE:CREATE TEMPORARY TABLE temp_table AS SELECT DISTINCT column1, column2 FROM table_name; SELECT * FROM temp_table;
使用窗口函數:SELECT column1, column2 FROM (SELECT column1, column2, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY column1, column2) AS rn FROM table_name) AS subquery WHERE rn = 1;