所以我有這個疑問完美的作品:MySQL JOIN/GROUP_CONCAT第二個表?
SELECT users.*,
GROUP_CONCAT(categories.category_name) AS categories
FROM users
LEFT OUTER JOIN user_categories ON users.user_id = user_categories.user_id
LEFT OUTER JOIN categories ON user_categories.category_id = categories.category_id
WHERE users.user_city = 'brooklyn'
GROUP BY users.user_id
LIMIT 10;
說我有保存的電話號碼,在「用戶」,用戶可以有任意數量的另一個電話號碼錶...我將如何去圍繞我正在做的完全相同的事情來分類?換句話說,我希望在「phones」表中找到具有相同「user_id」的所有phone_number,並將它們連接在一起(phone1,phone2,phone3)。我曾嘗試過:
SELECT users.*,
GROUP_CONCAT(phones.phone_number) AS phone_numbers,
GROUP_CONCAT(categories.category_name) AS categories
FROM users
LEFT OUTER JOIN phones ON users.user_id = phones.user_id
LEFT OUTER JOIN user_categories ON users.user_id = user_categories.user_id
LEFT OUTER JOIN categories ON user_categories.category_id = categories.category_id
WHERE users.user_city = 'brooklyn'
GROUP BY users.user_id
LIMIT 10;
沒有運氣...或至少查詢執行,但它做了一些奇怪的重複的事情......任何幫助將是可怕的!
謝謝!
好吧,這個按預期工作......只是想說明,在GROUP_CONCATS中有必要使用DISTINCT – mike 2010-03-01 19:20:24