gpt4 book ai didi

Mysql JOIN 子查询

转载 作者:行者123 更新时间:2023-11-29 13:18:59 26 4
gpt4 key购买 nike

我应该如何查询where

  • 我有(table1)行,在其他表中可以没有或有几行引用它
  • 这些引用(表 2)行的列 1 可以为 null 或日期

我想要 table1 中所有行 column1 不为空或根本没有行的所有行,在 table2 中。

当然基本的sql是这样的:

SELECT table1.* FROM table1 JOIN table2 ON table2.id = table1.table2_id

但是接下来会发生什么?

最佳答案

您可以计算查询中 null 的出现次数,例如 SUM(CASE WHEN table2.col IS NULL THEN 1 ELSE 0 END) AS nullcount,我假设 table2.col 是日期为 null 的那个

SELECT 
table1.*,
SUM(
CASE
WHEN table2.col IS NULL
THEN 1
ELSE 0
END
) AS nullcount
FROM
table1
JOIN table2
ON table2.id = table1.table2_id
HAVING nullcount > 0

关于Mysql JOIN 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108999/

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