dongtaijue1578 2014-08-27 06:38
浏览 49
已采纳

根据给定日期连接两个表时列出唯一行

I have two tables, one contains booking information another one container product information. When orders placed, a new record created in the booking table. I want to get a combined table with all columns from the two tables based on the give date.

table: bookings

 ------------------------------------------
| booth_number| booking_date |    status   |
|------------------------------------------|
|         1   |  2014-08-15  |    booked   |
|         2   |  2014-09-10  |    booked   |
|         3   |  2014-09-11  |    booked   |
 ------------------------------------------

table: products

 ---------------------------------------------------
| booth_number|     desc     |    locked   |  type  |
|------------------------------------------|--------|
|         1   | 2 x bottles  |    true     |  booth |
|         2   | 2 x bottles  |             |  booth |
|         3   | 4 x bottles  |             |  booth |
|         4   | 4 x bottles  |             |  booth |
|         5   | 5 x bottles  |             |  booth |
|         6   | 5 x bottles  |             |  booth |
|             |  $20 entry   |             | ticket |
 ---------------------------------------------------

After query, I want to get a result like below when request date is 2014-08-15

 -----------------------------------------------------------------------------
| booth_number|     desc     |    locked   |  type  | request_date |  status  |
|------------------------------------------|--------|--------------|----------|
|         1   | 2 x bottles  |    true     |  booth |  2014-08-15  |  booked  |
|         2   | 2 x bottles  |             |  booth |              |          |
|         3   | 4 x bottles  |             |  booth |              |          |
|         4   | 4 x bottles  |             |  booth |              |          |
|         5   | 5 x bottles  |             |  booth |              |          |
|         6   | 5 x bottles  |             |  booth |              |          |
 -----------------------------------------------------------------------------
  • 写回答

2条回答 默认 最新

  • dousha1394 2014-08-27 06:48
    关注
    SELECT
          *
    FROM products p
          LEFT JOIN bookings b
                      ON p.booth_number = b.booth_number
                            AND booking_date = '2014-08-15'
    WHERE p.booth_number IS NOT NULL
    ORDER BY p.booth_number
    ;
    

    This will list all booths if booked or not on a certain day

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?