如何在record while子句中显示其他数据为null?

时间:2017-11-29 09:15:17

标签: php mysql

我有一个出勤表

CREATE TABLE `msattendance` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `regnum` varchar(20) DEFAULT NULL,
    `reg_b` varchar(20) DEFAULT NULL,
    `in_hr` time NOT NULL,
    `out_hr` time NOT NULL,
    `in_info` varchar(20) DEFAULT NULL,
    `out_info` varchar(20) DEFAULT NULL,
    `in_date` date DEFAULT NULL,
    `out_date` date DEFAULT NULL,
    `in_mechine` varchar(3) DEFAULT NULL,
    `out_mechine` varchar(3) DEFAULT NULL,
    `upload_date` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `nik` (`nik`),
KEY `in_date` (`in_date`),
KEY `out_date` (`out_date`),
KEY `in_hr` (`in_hr`),
KEY `out_hr` (`out_hr`)
)

表上的示例值是:



+----+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+
| id |  regnum   | reg_b |  in_hr   |  out_hr  | in_info | out_info |  in_date   |  out_date  | in_mechine | out_mechine |     upload_time     |    upload_time2     | upload_date |
+----+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+
|  1 | 216115657 |  5657 | 07:36:00 | 15:17:00 | 1       | 2        | 2017-02-18 | 2017-02-18 | 2          | 2           | 2017-03-16 08:09:15 | 2017-03-16 08:11:42 | 2017-03-16  |
|  2 | 216115657 |  5657 | 00:00:00 | 18:59:00 | NULL    | 2        | NULL       | 2017-02-17 | NULL       | 2           | NULL                | 2017-03-16 08:09:15 | 2017-03-16  |
|  3 | 216115657 |  5657 | 00:00:00 | 07:41:00 | NULL    | 2        | NULL       | 2017-05-25 | NULL       | 4           | NULL                | 2017-05-30 15:32:45 | 2017-05-30  |
|  4 | 216115657 |  5657 | 07:41:00 | 16:38:00 | 1       | 2        | 2017-05-26 | 2017-05-26 | 2          | 2           | 2017-05-30 15:51:07 | 2017-05-30 16:01:39 | 2017-05-30  |
|  5 | 216115657 |  5657 | 00:00:00 | 16:40:00 | NULL    | 2        | NULL       | 2017-05-25 | NULL       | 4           | NULL                | 2017-05-30 15:51:07 | 2017-05-30  |
| 10 | 216115657 |  5657 | 07:44:00 | 16:45:00 | 1       | 2        | 2017-11-22 | 2017-11-22 | 2          | 4           | 2017-11-23 10:01:38 | 2017-11-23 10:07:04 | 2017-11-23  |
| 11 | 216115657 |  5657 | 07:43:00 | 16:41:00 | 1       | 2        | 2017-11-23 | 2017-11-23 | 2          | 3           | 2017-11-23 10:07:04 | 2017-11-24 13:13:27 | 2017-11-23  |
| 12 | 216115657 |  5657 | 07:43:00 | 16:33:00 | 1       | 2        | 2017-11-24 | 2017-11-24 | 3          | 4           | 2017-11-24 13:13:27 | 2017-11-25 09:50:05 | 2017-11-24  |
| 13 | 216115657 |  5657 | 00:00:00 | 14:13:00 | 1       | 2        | NULL       | 2017-11-25 | NULL       | 4           | 2017-11-25 09:50:05 | 2017-11-27 10:32:51 | 2017-11-25  |
| 14 | 216115657 |  5657 | 07:43:00 | 16:32:00 | 1       | 2        | 2017-11-27 | 2017-11-27 | 3          | 4           | 2017-11-27 10:32:51 | 2017-11-28 14:00:27 | 2017-11-27  |
| 15 | 216115657 |  5657 | 07:43:00 | 00:00:00 | 1       | NULL     | 2017-11-28 | NULL       | 4          | NULL        | 2017-11-28 14:00:27 | NULL                | 2017-11-28  |
+----+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+




我使用查询

SELECT * FROM `msattendance` WHERE (in_date between '2017-11-23' and '2017-11-28') and reg_b = '5657'

但不显示记录ID号13

我使用先生的sql代码。 Nigel Ren

SELECT * FROM `msattendance` 
 WHERE ((in_date between '2017-11-23' and '2017-11-28') 
         or in_date is null) 
     and reg_b = '5657'

但是范围之外的另一个日期显示为



+--------+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+
|   id   |  regnum   | reg_b |  in_hr   |  out_hr  | in_info | out_info |  in_date   |  out_date  | in_mechine | out_mechine |     upload_time     |    upload_time2     | upload_date |
+--------+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+
|   1493 | 216115657 |  5657 | 00:00:00 | 18:59:00 | NULL    | 2        | NULL       | 2017-02-17 | NULL       | 2           | NULL                | 2017-03-16 08:09:15 | 2017-03-16  |
| 100526 | 216115657 |  5657 | 00:00:00 | 07:41:00 | NULL    | 2        | NULL       | 2017-05-25 | NULL       | 4           | NULL                | 2017-05-30 15:32:45 | 2017-05-30  |
| 101727 | 216115657 |  5657 | 00:00:00 | 16:40:00 | NULL    | 2        | NULL       | 2017-05-25 | NULL       | 4           | NULL                | 2017-05-30 15:51:07 | 2017-05-30  |
| 297283 | 216115657 |  5657 | 00:00:00 | 14:13:00 | 1       | 2        | NULL       | 2017-11-25 | NULL       | 4           | 2017-11-25 09:50:05 | 2017-11-27 10:32:51 | 2017-11-25  |
| 293486 | 216115657 |  5657 | 07:44:00 | 16:45:00 | 1       | 2        | 2017-11-22 | 2017-11-22 | 2          | 4           | 2017-11-23 10:01:38 | 2017-11-23 10:07:04 | 2017-11-23  |
| 294748 | 216115657 |  5657 | 07:43:00 | 16:41:00 | 1       | 2        | 2017-11-23 | 2017-11-23 | 2          | 3           | 2017-11-23 10:07:04 | 2017-11-24 13:13:27 | 2017-11-23  |
| 296015 | 216115657 |  5657 | 07:43:00 | 16:33:00 | 1       | 2        | 2017-11-24 | 2017-11-24 | 3          | 4           | 2017-11-24 13:13:27 | 2017-11-25 09:50:05 | 2017-11-24  |
| 298601 | 216115657 |  5657 | 07:43:00 | 16:32:00 | 1       | 2        | 2017-11-27 | 2017-11-27 | 3          | 4           | 2017-11-27 10:32:51 | 2017-11-28 14:00:27 | 2017-11-27  |
| 299841 | 216115657 |  5657 | 07:43:00 | 00:00:00 | 1       | NULL     | 2017-11-28 | NULL       | 4          | NULL        | 2017-11-28 14:00:27 | NULL                | 2017-11-28  |
+--------+-----------+-------+----------+----------+---------+----------+------------+------------+------------+-------------+---------------------+---------------------+-------------+




如果提供in_date搜索,我可以在11月22日至11月28日期间显示表格内容,但输入日期为空?

1 个答案:

答案 0 :(得分:0)

这就是你需要的。

以下查询将返回in_date不存在或在给定日期范围内的所有用户。

SELECT *
FROM `msattendance` 
WHERE (in_date IS NULL OR in_date >= '2017-11-23')
AND in_date <= '2017-11-28'          
AND reg_b = '5657'