mysql - sql LEFT JOIN查询的使用方法

标签 mysql

我有两个表,分别是bookingCurrentDay_Booking

**booking**:

ID  bookingdate  TableID   starttime   endtime    userID   NumberOfPeople  phoneNumber  EmailID    
 1  2015-11-20   1         11:15:00    11:55:00   john     2               21029917     gmail.com    
 2  2015-11-20   1         11:15:00    11:40:00   riyas    2               21029917     gmail.com    
 3  2015-11-20   1         12:15:00    12:45:00   riyas    2               21029917     gmail.com
 4  2015-11-21   2         11:20:00    11:30:00   riyas    2               21029917     gmail.com

**CurrentDay_Booking**:

ID  bookingdate TableID    starttime   endtime    userID   NumberOfPeople  phoneNumber  EmailID    
 1  2015-11-20   1         11:15:00    11:55:00   john     2               21029917     gmail.com

我的问题是:需要将 booking 表中的数据放到 CurrentDay_Booking 中,而不是等同于 CurrentDay_Booking ID 和等同于当前日期和介于或等于当前时间的开始时间和结束时间

我使用不同的查询获得了当前时间和当前日期

我不知道如何使用 where 子句和 LEFT JOIN 进行查询

这是我的查询:

SELECT
    t.ID,
    t.bookingdate,
    t.TableID,
    t.starttime,
    t.endtime,
    t.userID,
    t.NumberOfPeople,
    t.phoneNumber,
    t.EmailID
FROM booking t
LEFT JOIN CurrentDay_Booking s 
ON t.ID = s.ID
WHERE s.ID is null
    and t.bookingdate='" . $cur_gmt_date . "'
    and t.starttime <= '" . $cur_gmt_time24 . "'
    and t.endtime => '" . $cur_gmt_time24 . "';

我认为我把条件放在了错误的地方。

请任何人帮助,在此先感谢。

最佳答案

根据您所描述的内容,按照您所描述的场景添加一个表来创建一个表没有任何意义。结果表是相同的,所以为什么不直接从包含相关特定日期所有预订的表中进行直接查询。不需要连接到另一个基于它是否已经存在的表

SELECT
      t.ID,
      t.bookingdate,
      t.TableID,
      t.starttime,
      t.endtime,
      t.userID,
      t.NumberOfPeople,
      t.phoneNumber,
      t.EmailID
   FROM 
      booking t
   WHERE 
      t.bookingdate='" . $cur_gmt_date . "'

有了基于 (bookingdate) 的“预订”表索引,它就不必比较已经存在或不存在的内容。这样您就不必担心为了开始新的一天而每天都要清除它。

关于mysql - sql LEFT JOIN查询的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33816881/

相关文章:

mysql - ServiceStack.OrmLite : StringLengthAttribute. MaxText 生成 "LONGTEXT"- 如何设置为 "TEXT"?

php - 验证 mysql 列数据并在该行中查找匹配的电子邮件?

mysql - 选择n个随机组,每组n行

php - 更新多行(PHP + MySQL)

javascript - 在javascript变量中获取php mysql结果

php - 如何使用 PHP 和 MySQLI 按相关性对结果进行排序?

mysql - 选择其他表中行数 > 0 的条目

mysql - 无法弄清楚为什么子查询返回超过 1 行

mysql - 从数据库中删除后,如何在运行时从组合框中删除项目

mysql - Docker-Compose 服务不通信