我有一個應用程序使用嵌套集模型類來組織我的數據,但我試圖寫一個查詢,將group_concat我的結果。我知道我需要在某處放置一些子選擇語句,但我無法弄清楚它!GROUP_CONCAT與嵌套集模型
這是我此刻的結構:
表:人
-----------+------------+-----------
|Person_ID | Name | Age |
-----------+------------+-----------
| 1 | Mark Vance | 19 |
| 2 | Michael Tsu| 22 |
| 3 | Mark Jones | 29 |
| 4 | Sara Young | 25 |
-----------+------------+-----------
表:person_to_group
----+------------+-----------
|ID | Person_ID | Group_ID |
----+------------+-----------
| 1 | 3 | 1 |
| 2 | 3 | 2 |
| 3 | 1 | 2 |
| 4 | 4 | 3 |
----+------------+-----------
表:組
----------+--------------+--------------+-------------
|Group_ID | Group_Name | Group_Left | Group_Right |
----------+--------------+--------------+-------------
| 1 | Root | 1 | 6 |
| 2 | Node | 2 | 5 |
| 3 | Sub Node | 3 | 4 |
----------+--------------+--------------+-------------
我需要呈現索姆ething這樣對我的結果:
//抓住了這個人的group_IDs並把它們放在類標籤...
<li class="2 3">Sara Young is in the Sub Node Group</li>
注意,雖然薩拉是子節點組中,她依然因爲她是節點的孩子,因此被賦予Node的id。
以下是我正在處理的查詢作爲起點。
SELECT *, GROUP_CONCAT(CAST(gg.Group_ID AS CHAR) SEPARATOR ' ') Group_IDs
FROM groups gg
LEFT JOIN person_to_group AS t1 ON gg.Group_ID = t1.Group_ID
LEFT JOIN person AS t2 ON t2.Person_ID = t1.Person_ID
GROUP BY t2.per_ID
ORDER BY t2.Name ASC
任何幫助將不勝感激!
非常感謝Bill! – Rich 2011-06-02 22:10:29