duanbu9345 2017-12-19 03:33
浏览 22
已采纳

当我在陈述时使用案例时,“双重结果”

Table 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       |
+--------+-----------+-------+----------+----------+------------+------------+

Table 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 |
+-----+------------+

i have a query for view attendance (msabsensi) data on database when in_date is null then use 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'

but a result was double

+-----------+------------+-----------+----------+----------+-----------+
| 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 |
+-----------+------------+-----------+----------+----------+-----------+

correct result is the second record how to display only that ?

+-----------+-----------+-----------+----------+----------+-----------+
| 11/1/2017 | 11/1/2017 | 11/2/2017 | 23:50:00 | 08:12:00 | 216065459 |
+-----------+-----------+-----------+----------+----------+-----------+
  • 写回答

2条回答 默认 最新

  • douhui1630 2017-12-19 03:46
    关注

    use COALESCE() or 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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件