我有如下的查詢條件顯示:
我希望最終顯示的效果如圖:<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140712/2014071209025946.png" alt="\">
將id相同的group_name合並到一行。實現方法:在group_name前面加關鍵字group_concat.然後在sql語句最後加上group by id.
group_concat能將同一列的值以逗號分隔開,拼成一個字符串。(mysql自帶)
第一個圖的sql語句如下:
SELECT tr.id ,tig.group_name FROM tj_record tr
INNER JOIN tj_item_group_ex tige ON tr.id = tige.record_id
INNER JOIN tj_item_group tig ON tige.id = tig.id
WHERE tr.id IN ("1407090008','1407090009') AND tige.id IN ('27','31')
SELECT tr.id ,GROUP_CONCAT(tig.group_name)
FROM tj_record tr
INNER JOIN tj_item_group_ex tige ON tr.id = tige.record_id
INNER JOIN tj_item_group tig ON tige.id = tig.id
WHERE tr.id IN ('1407090008','1407090009') AND tige.id IN ('27','31')
GROUP BY tr.id