书桌
bookID tripNo
b01 t01
b02 t02
b03 t03
旅行表
tripNo bookID seat1 seat1 seat2 seat3 seat4 seat5 seat6 seat7 seat8 seat9 seat10 seat11 seat11
t01 b01 booked booked NULL NULL booked booked booked booked NULL NULL NULL booked booked
t02 b02 NULL booked NULL NULL booked NULL booked NULL NULL booked NULL booked booked
t03 b03 booked booked NULL booked booked booked booked booked NULL booked booked booked booked
如何让它显示所有非NULL的座位?
预期结果
bookID Booked
b01 seat1,seat2,seat5,seat6,seat7,seat12,seat13
b02 seat2,seat5,seat7,seat10,seat12,seat13
答案 0 :(得分:1)
SQL查询返回一组固定的列,因此您无法有条件地删除列。所以,除非你采用动态SQL,否则你无法真正做到你想要的。
您的trip
表格格式笨拙。你应该有一张桌子TripSeats
,每次旅行和每个座位都有一排。
如果每个trip
只有11个席位,那么您可以在创建trip
时创建它们 - 只需使用NULL
值填充它们。
如果您这样做,则可以非常轻松地提取您想要的信息。