2016-07-11 99 views
1

所以這是我的查詢,截至目前:如何以逗號分隔的字符串連接表格?

SELECT *, 
JOIN users u 
JOIN user_addresses ua ON u.id = ua.userId_fk 
JOIN timeSlots ts ON u.id = ts.userId_fk 

我用這個Solr中索引的用戶。該時隙是這樣的:

ID | userId_fk | dayOfWeek | startTime | endTime 
1 100   MONDAY  10:00  12:00 

我怎樣才能將這個時隙數據作爲逗號分隔的列表或類似的東西,這樣我可以解析成一個JavaScript對象?期望輸出的

例子:

id | address | userName | timeslots 
1 123 ave someName MONDAY,10:00,12:00 TUESDAY,12:00,18:00 
+0

我不明白你的加入作爲一個逗號分隔的列表是什麼意思...什麼* *究竟是你想做什麼? – Siyual

+0

你用什麼來執行mysql?你應該能夠使用PHP(或者ASP,或者其他)來將結果轉儲到一個字符串中。 – durbnpoisn

+0

希望「理想輸出」永遠不會成爲表格 – Drew

回答

1

希望這將有助於

SELECT u.id , ua.address , userName , timeslots, concat(dayOfWeek, ',' ,startTime, ',' ,endTime) timeslots 
    JOIN users u 
    JOIN user_addresses ua ON u.id = ua.userId_fk 
    JOIN timeSlots ts ON u.id = ts.userId_fk 

您也可以GROUP_CONCAT它。如果你想多時隙與空間結合分離

SELECT u.id , ua.address , userName , timeslots, group_concat(concat(dayOfWeek, ',' ,startTime, ',' ,endTime) separator ' ') as timeslots 
    JOIN users u 
    JOIN user_addresses ua ON u.id = ua.userId_fk 
    JOIN timeSlots ts ON u.id = ts.userId_fk 
    group by userName