mySQLi显示列,列数据不是基于行的NULL

时间:2018-04-17 13:44:12

标签: php sql mysqli

书桌

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

1 个答案:

答案 0 :(得分:1)

SQL查询返回一组固定的列,因此您无法有条件地删除列。所以,除非你采用动态SQL,否则你无法真正做到你想要的。

您的trip表格格式笨拙。你应该有一张桌子TripSeats,每次旅行和每个座位都有一排。

如果每个trip只有11个席位,那么您可以在创建trip时创建它们 - 只需使用NULL值填充它们。

如果您这样做,则可以非常轻松地提取您想要的信息。