选择不同的列

时间:2013-01-18 22:14:46

标签: mysql sql

我有以下MYSQL:

SELECT DISTINCT(`user_id`), `type`, `link`, `add_text`
FROM `activity`  WHERE `id` != 0 AND `type` !=6 AND (`type` = 4 OR `type` = 5)
ORDER BY `id` DESC  LIMIT 4

我只想要唯一的user_id,但我在sql结果中得到了重复。对此sql进行的正确修改是什么,以便不重复user_id s

2 个答案:

答案 0 :(得分:1)

如果您不关心每个用户的类型,链接或添加文本,您可以使用min / max和group by。

SELECT user_id, max(type), max(link), max(add_text)
FROM activity
WHERE id != 0 A
AND type in (4,5) 
Group by User_Id 
ORDER BY id DESC  LIMIT 4

如果你关心你可以group_concat类型,链接和添加文本

SELECT user_id, Group_Concat(type), Group_Concat(link), Group_Concat(add_text) 
FROM activity
WHERE id != 0 A
AND type in (4,5) 
Group by User_Id
ORDER BY id DESC  LIMIT 4

edoud group by shoudl只能在user_ID

答案 1 :(得分:0)

SELECT GROUP_CONCAT(DISTINCT `user_id`) user_ids, `type`, `link`, `add_text`
FROM `activity`
WHERE `id` != 0 AND `type` IN (4,5)
GROUP BY `type`, `link`, `add_text` DESC  LIMIT 4;