mysql - 如何使用 MySql 根据时间间隔获取 id

标签 mysql node.js select left-join

我想根据中间时间获取cubbersId

这是我的表:表名cubbersavailability

+---------------------------------------+
|cubbersId  |   fromTime    |   toTime   |
+---------------------------------------+
|   1       |   09:30:00    |   18:30:00 |
|   1       |   09:30:00    |   18:30:00 |
|   1       |   09:30:00    |   18:30:00 |
|   1       |   09:30:00    |   12:30:00 |
+----------------------------------------+

在上表中,cubbersIsd 有一些时间。

条件

  1. 如果我选择 fromTime 作为 10:30:00toTime 作为 12:00:00 意味着获取最后 1 列
  2. 如果我选择 fromTime 作为 08:30:00toTime 作为 16:00:00 意味着不要选择列
  3. 如果我选择 fromTime 作为 10:30:00toTime 作为 20:00:00 意味着不要选择列
  4. 如果我选择 fromTime 作为 08:30:00toTime 作为 20:00:00 意味着不要选择列

我试过这个查询,但不支持:

SELECT 
    c.cubbersId, 
    shopAvailTime.fromTime, 
    shopAvailTime.toTime 
FROM cubbers c 
    LEFT JOIN 
        (SELECT * FROM cubbersavailability ca WHERE CAST('08:30:00' as time) < ca.toTime AND CAST('12:00:00' as time) > ca.fromTime ) as shopAvailTime ON (c.cubbersId = shopAvailTime.cubbersId) 
WHERE c.cubbersId = 1

但是它得到了所有的列表 enter image description here

Note: this question already asked but that not help my question;

最佳答案

SELECT x.cubbersId
     , x.fromTime
     , x.toTime 
  FROM cubbersavailability x 
 WHERE '08:30:00' >= x.fromTime
   AND '12:00:00' <= x.toTime
   AND x.cubbersId = 1

关于mysql - 如何使用 MySql 根据时间间隔获取 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56390119/

相关文章:

angularjs - 如何使用 loopback api explorer 上传文件?

ruby-on-rails - 如何在Ruby on Rails 3中控制选择框的大小?

php - PDO 从动态 html 表插入数据库值

mysql - 如何使用带有json数据的nodejs将json数据插入mysql表

php - 遍历数组并根据列中的空值插入或更新列 - MySQL

javascript - 我可以只使用 Node 来抓取这个网站吗?

mysql - 城镇报告(订单数)

mysql - 在 MySQL 中计算转化率

sql - Once SQL 命令判断真或假

mysql - 来自多个表的多次计数并按年级分组