如何在sql中添加日期选择

时间:2016-08-21 04:22:13

标签: mysql sql node.js momentjs

我希望能够选择在接下来的两个小时之前或之后开始的课程,直到明天结束,但我不知道该怎么做。

我当前的sql在下面,我只能选择在今天的日期之后和当前时间之后的类。我希望能够只返回从现在开始直到明天结束的两个小时开始的课程。

我能够将当前日期和当前时间传递到我将构造sql语句的node.js函数中。另外,如果我需要使用它,我可以访问moment.js库。

任何帮助都将不胜感激。

SELECT DISTINCT b.name
              , a.time 
           FROM class a 
           Inner join (SELECT class_id, count(clientid) 
                       FROM bookings  
                       GROUP BY class_id
                       HAVING count(clientid) < 10) as openClasses on        
                a.class_id = openClasses.class_id
           JOIN class_detail b 
             ON a.class_id = b.id   
           JOIN branch c 
             ON a.branch_id = c.id 
          WHERE c.level <= ( SELECT d.level 
                               FROM client d 
                              WHERE d.facebook_id = 'xxxxxx'
                           )
            AND a.date = '2016-08-17' 
            AND a.time >= '13.00.00';

我的表格如下:

BOOKINGS
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | bigint(20)  | NO   | PRI | NULL    |       |
| CLIENT_ID | int(11)     | NO   |     | NULL    |       |
| CLASS_ID  | int(11)     | NO   |     | NULL    |       |
| STATUS    | varchar(10) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

mysql> show fields from BRANCH;
+---------------------+-------------+------+-----+---------+----------------+
| Field               | Type        | Null | Key | Default | Extra          |
+---------------------+-------------+------+-----+---------+----------------+
| id                  | int(10)     | NO   | PRI | NULL    | auto_increment |
| NAME                | char(50)    | NO   |     | NULL    |                |
| CONTACT_NO          | char(50)    | YES  |     | NULL    |                |
| MAP_IMG_PATH        | char(200)   | YES  |     | NULL    |                |
| ADDRESS             | char(200)   | YES  |     | NULL    |                |
| LEVEL               | int(2)      | NO   |     | NULL    |                |
| LOCATION            | int(10)     | YES  |     | NULL    |                |
| SECTOR_NAME         | varchar(45) | YES  |     | NULL    |                |
| SECTOR_MAP_IMG_PATH | char(200)   | YES  |     | NULL    |                |
+---------------------+-------------+------+-----+---------+----------------+



mysql> show fields from CLIENT;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id           | int(10)     | NO   | PRI | NULL    |       |
| NAME         | char(50)    | NO   |     | NULL    |       |
| DOB          | int(8)      | NO   |     | NULL    |       |
| LOCAL_BRANCH | int(10)     | YES  |     | NULL    |       |
| FACEBOOK_ID  | char(50)    | NO   |     | NULL    |       |
| START_DATE   | int(8)      | NO   |     | NULL    |       |
| EMAIL        | char(50)    | YES  |     | NULL    |       |
| PIN          | int(4)      | YES  |     | NULL    |       |
| END_DATE     | int(8)      | NO   |     | NULL    |       |
| LEVEL        | int(2)      | YES  |     | NULL    |       |
| TEL          | varchar(20) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+



mysql> show fields from CLASS_DETAIL;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(10)      | NO   | PRI | NULL    |       |
| NAME         | char(50)     | NO   |     | NULL    |       |
| DESCRIPTION  | char(200)    | NO   |     | NULL    |       |
| CATEGORY     | varchar(4)   | YES  |     | NULL    |       |
| ACHIEVE_TYPE | char(200)    | YES  |     | NULL    |       |
| IMG_M        | varchar(200) | YES  |     | NULL    |       |
| IMG_F        | varchar(200) | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+



mysql> show fields from CLASS;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| CLASS_ID  | int(10) | YES  |     | NULL    |                |
| BRANCH_ID | int(10) | NO   |     | NULL    |                |
| DURATION  | int(3)  | YES  |     | NULL    |                |
| DATE      | date    | NO   |     | NULL    |                |
| TIME      | time    | NO   |     | NULL    |                |
| STATUS    | char(1) | NO   |     | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
7 rows in set (0.11 sec)

0 个答案:

没有答案