2013-02-11 84 views
-1

我有兩個表這樣顯示GROUP_CONCAT空記錄

profile_answers

+---------+------------+ 
|  id | class_name | 
+---------+------------+ 
|  1 | Class 1 | 
|  2 | Class 2 | 
|  3 | Class 1 | 
+---------+------------+ 

的教育

+---------+--------------------+------------+ 
|  id | profile_answers_id | sample | 
+---------+--------------------+------------+ 
|  1 | 1     |  1234 | 
|  2 | 1     |  2334 | 
|  3 | 1     |  3434 | 
+---------+------------+--------------------+ 

我跑的查詢,

select educations.profile_answer_id, GROUP_CONCAT(educations.sample) from educations 
LEFT JOIN profile_answers ON educations.profile_answers_id = profile_answers.id 

+--------+--------------------+-------------+ 
|  id | sample       | 
+---------+--------------------+------------+ 
|  1 | 1234,2334,3434     | 
+---------+------------+--------------------+ 

其實我是想,

+--------+--------------------+-------------+ 
|  id | sample       | 
+---------+--------------------+------------+ 
|  1 | 1234,2334,3434     | 
|  2 | NULL       | 
|  3 | NULL       | 
+---------+------------+--------------------+ 

我試過了,ISNULL,IFNULL與group_by profile_answers.id這給了我正確的結果組合,但它竟然是相當昂貴運行時間條款

+0

給出給出正確結果的昂貴查詢? – Dukeling 2013-02-11 20:35:35

+0

你剛纔問這個問題... – sgeddes 2013-02-11 20:36:46

+0

http://dpaste.com/919899/ ..在這個查詢上花費了15秒 – 2013-02-11 20:40:15

回答

2

這是我在你上一個問題中的回答...

看起來像你缺少你GROUP BY:

select profile_answers.id, GROUP_CONCAT(educations.sample) 
from profile_answers 
LEFT JOIN educations ON educations.profile_answers_id = profile_answers.id 
GROUP BY profile_answers.id 

我也改變了自己的加入,使profile_answers表的主表。

祝你好運。