2013-08-23 77 views
0

簡單的問題一個SELECT:CONCAT組中的另一SELECT

有2個表:

  1. 流派[名稱,songID]

  2. 宋[ID,標題,用戶ID,狀態]

    SELECT id,name,title,userID,status FROM songs INNER JOIN genre ON song.id = genre.songID ORDER BY id ASC;

什麼是查詢從

+----+-------------+----------------------+--------+--------+ 
| id | genre.name | song.title   | userID | status | 
+----+-------------+----------------------+--------+--------+ 
| 1 | tech  | Feel it all   |  1 |  1 | 
| 2 | tech  | Tester    |  1 |  1 | 
| 3 | music  | Sejujurnya   |  1 |  1 | 
| 4 | music  | Not Done    |  1 |  1 | 
| 5 | life  | Cinta    |  1 |  1 | 
| 6 | life  | Feel it all   |  1 |  1 | 
| 7 | life  | Not Done    |  1 |  1 | 
| 8 | truth  | Tester    |  1 |  1 | 
| 9 | tree  | Tester    |  1 |  1 | 
| 10 | climb  | Tester    |  1 |  1 | 
+----+-------------+----------------------+--------+--------+ 

+----+-------------+---------------------------------+--------+--------+ 
| id | genre.name | song.title      | userID | status | 
+----+-------------+---------------------------------+--------+--------+ 
| 1 | tech  | Feel it all,Tester    |  1 |  1 | 
| 2 | music  | Sejujurnya, Not Done   |  1 |  1 | 
| 3 | life  | Cinta, Feel it all, Note Done |  1 |  1 | 
| 4 | truth  | Tester       |  1 |  1 | 
| 5 | tree  | Tester       |  1 |  1 | 
| 6 | climb  | Tester       |  1 |  1 | 
+----+-------------+---------------------------------+--------+--------+ 

感謝

+0

可能的重複http://stackoverflow.com/questions/4650744/mysql-multiple-row-to-single-row –

+0

我想我看到一個模式,來自同一流派的標題是內爆的,但然後'暗夜'和'布卡'有什麼關係?他們甚至不在原來的桌子上。 –

+1

查看MySQL中的[GROUP_CONCAT-function](http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat)。 @Declan_K:你的鏈接是針對SQL服務器的,稍有不同。 –

回答

1

得到的結果使用GROUP_CONCAT與GROUP BY

SELECT 
    id, 
    genre.name, 
    GROUP_CONCAT(title) as title, 
    userID, 
    status 
FROM 
    songs 
INNER JOIN 
    genre 
ON 
    song.id=genre.songID 
GROUP BY 
    genre.name 
ORDER BY 
    id ASC 
+0

了不起的工作朋友。真棒。 – AFwcxx

+0

@AFwcxx這裏返回的'id'字段在這裏沒有任何意義,因爲它只涉及其中一首歌曲,您可以將concat更改爲'GROUP_CONCAT(CONCAT(id,「 - 」,title))作爲標題, '這樣你仍然可以得到帶有標題的歌曲的ID,並將它們分割成你的代碼,即'explode(「 - 」,$ title);' –

0
SELECT 
    `Songs`.`id`, 
    `Genre`.`Name`, 
    GROUP_CONCAT(`Songs`.`Title`) as Title, 
    `Songs`.`userID`, 
    `Songs`.`status` 
FROM 
    `Genre`, 
    `Songs` 
WHERE 
    `Genre`.`SongID` = `Songs`.`id` 
GROUP BY 
    `Genre`.`Name`