有的时候需要对特定的字段进行去重处理,但是distinct关键词又不能用在后边,如:
SELECT id, name, distinct(users.target_id, users.target_type) FROM users; #(error)
一个技巧是:
SELECT users.*, COUNT(distinct(users.target_id, users.target_type)) from users;
但是实际应用中发现上面的做法不是很好用!因为当返回记录为空的时候,居然也返回空记录, 于是试着用 group by 来实现:
SELECT users.* from users GROUP BY users.target_id, users.target_type;