gpt4 book ai didi

php - "Double Result"当我使用 case when 语句时

转载 作者:行者123 更新时间:2023-11-29 10:28:24 26 4
gpt4 key购买 nike

表 msabsensi

+--------+-----------+-------+----------+----------+------------+------------+
| id | nik | nik_b | in_hr | out_hr | in_date | out_date |
+--------+-----------+-------+----------+----------+------------+------------+
| 262230 | 216065459 | 5459 | 07:42:00 | 16:37:00 | 2017/10/25 | 2017/10/25 |
| 263430 | 216065459 | 5459 | 07:40:00 | 16:29:00 | 2017/10/26 | 2017/10/26 |
| 264610 | 216065459 | 5459 | 07:38:00 | 20:01:00 | 2017/10/27 | 2017/10/27 |
| 267550 | 216065459 | 5459 | 19:40:00 | 08:38:00 | 2017/10/29 | 2017/10/30 |
| 268870 | 216065459 | 5459 | 23:50:00 | 09:06:00 | 2017/10/30 | 2017/10/31 |
| 270067 | 216065459 | 5459 | 00:00:00 | 08:32:00 | NULL | 2017/11/01 |
| 271359 | 216065459 | 5459 | 23:50:00 | 08:12:00 | 2017/11/01 | 2017/11/02 |
| 272614 | 216065459 | 5459 | 23:48:00 | 08:47:00 | 2017/11/02 | 2017/11/03 |
| 274119 | 216065459 | 5459 | 00:00:00 | 20:10:00 | NULL | 2017/11/04 |
| 274975 | 216065459 | 5459 | 07:46:00 | 00:00:00 | 2017/11/05 | NULL |
+--------+-----------+-------+----------+----------+------------+------------+

表mstanggal

+-----+------------+
| id | tanggal |
+-----+------------+
| 298 | 2017/10/25 |
| 299 | 2017/10/26 |
| 300 | 2017/10/27 |
| 301 | 2017/10/28 |
| 302 | 2017/10/29 |
| 303 | 2017/10/30 |
| 304 | 2017/10/31 |
| 305 | 2017/11/01 |
| 306 | 2017/11/02 |
| 307 | 2017/11/03 |
| 308 | 2017/11/04 |
| 309 | 2017/11/05 |
+-----+------------+

当 in_date 为空时,我有一个查询,用于查看数据库中的出勤 (msabsensi) 数据,然后使用 out_date

SELECT c.tanggal, b.in_date, b.out_date, b.in_hr, b.out_hr, b.nik from mstanggal c
left outer join msabsensi b on c.tanggal = (CASE WHEN c.tanggal = b.in_date THEN b.in_date ELSE b.out_date END)
where c.tanggal = '2017-11-01' and b.nik = '216065459'

但结果是双倍的

+-----------+------------+-----------+----------+----------+-----------+
| tanggal | in_date | out_date | in_hr | out_hr | nik |
+-----------+------------+-----------+----------+----------+-----------+
| 11/1/2017 | 10/31/2017 | 11/1/2017 | 23:46:00 | 08:32:00 | 216065459 |
| 11/1/2017 | 11/1/2017 | 11/2/2017 | 23:50:00 | 08:12:00 | 216065459 |
+-----------+------------+-----------+----------+----------+-----------+

正确结果是第二条记录如何只显示那个?

+-----------+-----------+-----------+----------+----------+-----------+
| 11/1/2017 | 11/1/2017 | 11/2/2017 | 23:50:00 | 08:12:00 | 216065459 |
+-----------+-----------+-----------+----------+----------+-----------+

最佳答案

使用COALESCE()或IFNULL()

SELECT c.tanggal
, b.in_date
, b.out_date
, b.in_hr
, b.out_hr
, b.nik
FROM mstanggal c
LEFT JOIN msabsensi b ON c.tanggal = coalesce(b.in_date, b.out_date)
WHERE c.tanggal = '2017-11-01' AND b.nik = '216065459'

关于php - "Double Result"当我使用 case when 语句时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47879661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com