dqalnwuci494308 2018-04-17 13:44
浏览 38
已采纳

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

Book Table

bookID    tripNo
b01         t01
b02         t02
b03         t03

Trip Table

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  

how do i make it like show all the seat that is not NULL?

Expected Result

bookID   Booked
b01       seat1,seat2,seat5,seat6,seat7,seat12,seat13
b02       seat2,seat5,seat7,seat10,seat12,seat13
  • 写回答

1条回答 默认 最新

  • dpje52239 2018-04-17 13:50
    关注

    A SQL query returns a fixed set of columns, so you cannot conditionally remove a column. So, you can't really do what you want, unless you resort to dynamic SQL.

    Your trip table has an awkward format. You should really have a table TripSeats with one row per trip and per seat.

    If each trip has exactly 11 seats, then you can create them when the trip is created -- just populate them with NULL values.

    If you did this, it would be very easy to extract the information you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil编译时报错"no source": Error: #5: cannot open source
  • ¥50 操作系统时间无法更新
  • ¥20 Windows11, usb转hdmi,外接显示器无反应
  • ¥20 怎么在JavaFx的TableView中动态地添加数据。
  • ¥15 MFC里的工具栏按钮图标使用外部图片
  • ¥15 如何在 llama.cpp 服务器中实现用户登录功能的优化方案?(语言-c++)
  • ¥15 有会用octave 的吗,急!代做!好偿!
  • ¥15 有一套同城小程序源码,Uniapp前端,php+html+mysql后端 ,能不能教我搭建起来可以运行,我不知道怎样操作
  • ¥15 mac调用java.io接口无法在根目录生成文件
  • ¥15 java微服务节点假死,网关路由时长延迟