2011-11-29 67 views
0

我有一個表 「答案」 是這樣的:SQL查詢 - 集團通過不起作用

Id, Registration_id, attendee_id, question_id, answer 
12 Reg02    1    1  Paul 
13 Reg02    1    2  Smith 
14 Reg03    2    1  Rachel 
15 Reg03    2    2  Silva 

我的查詢是:

Select attendee_id, answer 
From wp_events_answer;        

結果我得到這樣的:

attendee_id answer 
    1   Paul 
    1   Smith 
    2   Rachel  
    2   Silva 

,但我想有這樣的:

attendee_id FirstName Name 
    1   Paul  Smith 
    2   Rachel  Silva 

我加了Group By attendee_id。但它仍然不起作用。因此它只顯示:

attendee_id FirstName 
1    Paul 
2    Rachel 

你能告訴我什麼是錯的嗎?

感謝

+0

我認爲此組是工作的罰款... – evilone

+3

你想要的是不羣的功能,通過 –

+0

您不能按一列,並選擇其他非聚集列值。我認爲mySql可以讓你,但你得到的結果最好是隨機的。 –

回答

0

我認爲你正在尋找GROUP_CONCAT

select 
    attendee_id, 
    group_concat(answer ORDER BY Id 
       SEPERATOR ' ') 
from wp_events_answer 
group by attendee_id 
2

如果你需要從表中有多行中,你需要加入表本身的結果展現出來。

select fn.attendee_id, fn.answer as FirstName, ln.answer as lastName FROM wp_events_answer as fn INNER JOIN wp_events_answer as ln WHERE (fn.attendee_id = ln.attendee_id) and (fn.question_id = 1) and (ln.question_id=2) 
5

你不需要一組,你需要一個自聯接。

SELECT f.attendee_id, f.answer as FirstName, l.answer as Name 
FROM wp_events_answer f 
    JOIN wp_events_answer l ON f.attendee_id = l.attendee_id 
WHERE f.question_id = 1 AND l.question_id = 2