我有3个表(tbl_reservations / tbl_series / tbl_rooms),如何将匹配的房间合并为1列,用空格或“|”分隔,以及来自预订表的数据?
tbl_reservations
------------
id, startdate, enddate, series
1, 2014-05-20, 2014-05-22, 1
2, 2014-05-24, 2014-05-25, 2
tbl_series
--------
reservation, room
1, 1
1, 3
1, 4
2, 1
2, 2
tbl_rooms
-----
id, name
1, room a
2, room b
3, room c
4, room d
当我需要回归的时候是这样的......
startdate, enddate, rooms
2014-05-20, 2014-05-22, 1|3|4
2014-05-24, 2014-05-25, 1|2
答案 0 :(得分:1)
select r.id,
r.startdate,
r.enddate,
GROUP_CONCAT(s.room order by s.room separator '|' ) as rooms
from tbl_reservations r
join tbl_series s on s.reservation = r.series
group by r.id, r.startdate, r.enddate
请参阅SqlFiddle
答案 1 :(得分:0)
你想把这一切都放在一个栏目中吗?在这种情况下,这是一个副本:
MySQL, Concatenate two columns
或
MySQL combine two columns and add into a new column
或者你想在查询后有一个表,它将在每个单独的预留行中。在这种情况下,您需要查看使用
INNER JOIN
这样的功能:
Select * from tbl_rooms t1
inner join
(select * from tbl_reservations st1
inner join
(select * from tblseries) st2
on st1.id=st2.reservation
) t2 on t1.id = t2.room