需求:對一張用戶表根據name/email/card_num字段去除重複數據;函數
思路:用group by方法能夠查詢出'去重'後的數據,將這些數據存儲到一張臨時表中,而後將臨時表的數據存儲到指定的表中;測試
誤區及解決方案:group by方法只能獲取部分字段(去重指定字段),不能一次獲取到完整的數據,可是能夠經過max函數獲取group by結果集中的id,再根據id集合查詢出所有的記錄。code
SELECT max(id) as id,name,email,card_num FROM users GROUP BY name,email,card_num;
email
SELECT ID from (SELECT max(id) as id,name,email,card_num FROM users GROUP BY name,email,card_num) as T;
select
SELECT * from users where id in (SELECT ID from (SELECT max(id) as id,name,email,card_num FROM users GROUP BY name,email,card_num) as T);
方法
create TEMP TABLE tmp_data as SELECT * from users where id in (SELECT ID from (SELECT max(id) as id,name,email,card_num FROM users GROUP BY name,email,card_num) as T);
數據
insert into users_copy1 select * from tmp_data;
查詢
select count(*) from users_copy1;
集合