Mysql JOIN子查询

编程入门 行业动态 更新时间:2024-10-25 00:31:47
本文介绍了Mysql JOIN子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我应该如何查询哪里

  • 我有(table1)行,在其他表中可以没有任何行或多个行引用该行
  • 这些引用(表2)行的colum1可以为null或日期

我想让table1中的所有行的table2中的所有列column1都不为null或根本没有行.

I would like to have all rows from table1 which have all rows column1 not as null or no rows at all, in table2.

当然,基本的sql就像这样:

Of course the basic sql goes like:

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

但是接下来会发生什么呢?

But what comes next?

推荐答案

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

You can count the occurences of null in your query like SUM(CASE WHEN table2.col IS NULL THEN 1 ELSE 0 END) AS nullcount, i assume table2.col is the one which has date of null in it

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子查询

本文发布于:2023-06-01 10:27:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/412944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mysql   JOIN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!