2016-09-29 110 views
2

我有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但不知道如何有效地使用它。

回答

1

試試這個:

SELECT user_name,   
     CASE WHEN COUNT(*) > 1 
      THEN SUBSTRING_INDEX(GROUP_CONCAT(date_limit ORDER BY date_limit), 
           ',', COUNT(*) - 1) 
      ELSE '' 
     END AS dates,    
     COUNT(*) AS count_dates 
FROM users as PP 
INNER JOIN `limit` as PAL ON PP.user_id = PAL.user_id 
LEFT JOIN limit_setting as ESL ON ESL.limit_id = PAL.limit_id 
GROUP BY user_name 

查詢使用SUBSTRING_INDEX功能,以獲得通過GROUP_CONCAT返回的所有日期除外的最後日期。在GROUP_CONCAT內使用ORDER BY,我們可以在最後放置最大日期,以便SUBSTRING_INDEX恰好截斷此日期。

Demo here

+0

謝謝你這麼多的幫助和解釋......我從來沒有使用'SUBSTRING_INDEX'之前,並不知道它...再次感謝:) – user2595861

0

我會用一個子查詢返回按極限的最大日期和加入這一回限制設置表,以消除來自group_concat()最大日期與case表達:

SELECT 
    PP.`user_name`, 
    count(ESL.Limit_id) as count_dates, 
    GROUP_CONCAT(CASE 
        WHEN ESL.date_limit<>ls.maxdate THEN ESL.date_limit 
        ELSE '' 
       END 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 
LEFT JOIN (SELECT LimitId, MAX(date_limit) as maxdate 
      FROM limit_setting 
      GROUP BY LimitId 
     ) ls ON ESL.LimitId=ls.LimitId 
GROUP BY PP.`user_name` 
+0

我得到了這個結果,但與','在'日期'字段的開始處...像這樣:',2016-09-23 00:00:00,2016-09-21 00:00:00,20 16-09-2..' – user2595861