社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

mysql case不返回空值

Gavin • 5 年前 • 1695 次点击  

我有以下疑问:

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
 
1695 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Fahmi
Reply   •   1 楼
Fahmi    6 年前

你必须把 t1.collection_date <= '2019-02-09' 至于作业ID 1002日期是 '2019-02-09'

SELECT  t1.id,
        t1.collection_date,
        t2.order_date,
        t2.hasOrders
FROM job_details AS t1
    LEFT JOIN
        (
        SELECT  job_id, 
                MAX(date) AS order_date,
                (CASE COALESCE(MAX(date), '0000-00-00') WHEN '0000-00-00' THEN 'false' ELSE 'true' END) AS hasOrders
        FROM sales_orders group by job_id
        )
    AS t2
    ON t2.job_id = t1.id
WHERE t1.collection_date <= '2019-02-09'
Erik Reder
Reply   •   2 楼
Erik Reder    6 年前

我认为,通过将“max”和group by移到外部语句,逻辑至少应该可以正常工作(如果还想计算该列,则需要添加“coalesce”语句)。不确定表现如何,但我会先看看结果是否总体上是好的。希望语法没问题,没测试过。

SELECT  t1.id,
        t1.collection_date,
        MAX(t2.last_order_date)
FROM job_details AS t1
        LEFT JOIN
        (
        SELECT  job_id, 
                last_order_date
        FROM sales_orders
        )
    AS t2
    ON t2.job_id = t1.id
GROUP BY t1.id
Joakim Danielson
Reply   •   3 楼
Joakim Danielson    6 年前

您需要将case when移动到外部查询,因为在 sales_orders

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