我有3个表格,我正在加入他们以获取一些数据。GROUP_CONCAT选项中的条件
----------------- Table Name: users ------------------------------- |user_id | user_name | ------------------------------- 123 | abc ------------------------------- 223 | bcd ------------------------------- 323 | cde ------------------------------- ----------------- Table Name: limit ------------------------------- user_id | limit_id ------------------------------- 123 | 1 ------------------------------- 223 | 2 ------------------------------- 323 | 3 ------------------------------- 323 | 4 ------------------------------- ------------------------- Table Name: limit_setting ------------------------------- limit_id | date_limit ------------------------------- 1 | 2016-09-29 12:00:00 ------------------------------- 2 | 2016-09-28 12:00:00 ------------------------------- 3 | 2016-09-27 12:00:00 ------------------------------- 1 | 2016-09-27 12:00:00 ------------------------------- 1 | 2016-09-24 12:00:00 ------------------------------- 4 | 2016-09-25 12:00:00 ------------------------------- 4 | 2016-09-26 12:00:00 -------------------------------
我需要得到这样的结果。我被日期栏的GROUP_CONCAT卡住了。 日期列应包含除MAX日期以外的所有条目。如果在limit_id的limit_setting
表中只有一个条目,那么它不应该为该用户显示任何内容。 count_dates
:它在limit_setting
表中存在的条目数。
Desired output ---------------------------------------------------------------------- user_name | dates | count_dates ---------------------------------------------------------------------- abc | 2016-09-27 12:00:00 , 2016-09-24 12:00:00 | 3 ---------------------------------------------------------------------- bcd | | 1 ---------------------------------------------------------------------- cde | | 1 ----------------------------------------------------------------------- cde | 2016-09-26 12:00:00 | 2 -----------------------------------------------------------------------
SELECT PP.`user_name`, count(ESL.Limit_id) as count_dates,
GROUP_CONCAT(ESL.date_limit SEPARATOR ',') as dates
FROM users as PP INNER JOIN `limit` as PAL ON PP.Id = PAL.PlayerId
LEFT JOIN limit_setting as ESL ON ESL.LimitId = PAL.limitId
GROUP BY PAL.limitId
此外,我试着用(这什么也没有返回)
SELECT ESL.date_limit, MAX(date_limit) as max_date, PP.`user_name`, count(ESL.Limit_id) as count_dates,
GROUP_CONCAT(ESL.date_limit SEPARATOR ',') as dates
FROM users as PP INNER JOIN `limit` as PAL ON PP.Id = PAL.PlayerId
LEFT JOIN limit_setting as ESL ON ESL.LimitId = PAL.limitId
GROUP BY PAL.limitId
HAVING ESL.date_limit > max_date
我试着用Find_in_set
但不知道如何有效地使用它。
谢谢你这么多的帮助和解释......我从来没有使用'SUBSTRING_INDEX'之前,并不知道它...再次感谢:) – user2595861