Py学习  »  DATABASE

mysql case不返回空值

Gavin • 5 年前 • 1693 次点击  

我有以下疑问:

SELECT  t1.id,
        t1.collection_date,
        t2.last_order_date,
        t2.hasOrders
FROM job_details AS t1
    LEFT JOIN
        (
        SELECT  job_id, 
                MAX(date) AS last_order_date,
                (CASE COALESCE(MAX(date), '0000-01-01') WHEN '0000-01-01' THEN 'false' ELSE 'true' END) AS hasOrders
        FROM sales_orders
        GROUP BY job_id
        )
    AS t2
    ON t2.job_id = t1.id

我在期待 t2.hasOrders 要么是 true false CASE 部分 LEFT JOIN 但我一直在 真的 NULL 作为 demonstrated here

我知道 t2.date 将始终返回有效日期或默认列值 0000-00-00 .

这是我的查询的简化版本,因为 左连接 查询是一个临时表。

为什么 无效的 值为 T2日期 不返回我的预期结果?

提前谢谢。

编辑1

链接到演示-已删除 where 部分查询

编辑2

预期结果:

id      collection_date     last_order_date     hasOrders
1001    2019-02-07          2019-02-01          true
1002    2019-02-09          0000-01-01          false

实际结果:

 id     collection_date     last_order_date     hasOrders
1001    2019-02-07          2019-02-01          true
1002    2019-02-09          NULL                NULL

编辑3

更新以上查询以包括 group by 在里面 left join 部分查询和更改 000—00—00 0000-01-01 如建议。仍然没有按预期工作。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40912
 
1693 次点击  
文章 [ 3 ]  |  最新文章 5 年前