mysql外部连接

编程入门 行业动态 更新时间:2024-10-26 17:18:49
mysql外部连接 - 确定连接的行是否存在(mysql outer join - determine if the joined row exists)

我有两个主键相同的表,但其中一个比另一个大得多。 我想知道哪个id在较小的表中有一行。 (在这个例子中, a很大, b很小)。 现在,我使用带CASE的OUTER JOIN来确定b值是否为NULL。 它不工作(总是得到1)。 解决这个问题很好,但有一个更好的方法。 我应该怎么做?

SELECT a.id, CASE b.id WHEN NULL THEN 0 ELSE 1 END AS exists FROM a LEFT OUTER JOIN b ON a.id=b.id;

I have two tables with the same primary key, but one is much much larger than the other. I want to know which ids have a row in the smaller table. (In the example, a is large and b is small). Right now, I'm using an OUTER JOIN with a CASE to determine if the b value is NULL or not. It's not working (always getting 1). Fixing this would be fine, but there's got to be a better way. How should I do it?

SELECT a.id, CASE b.id WHEN NULL THEN 0 ELSE 1 END AS exists FROM a LEFT OUTER JOIN b ON a.id=b.id;

最满意答案

这与您显示的内容具有相同的逻辑,但代码较短:

SELECT a.id,NOT ISNULL(b.id) AS exists FROM a LEFT OUTER JOIN b ON a.id=b.id;

this has the same logic of what you showed but has a shorter code:

SELECT a.id,NOT ISNULL(b.id) AS exists FROM a LEFT OUTER JOIN b ON a.id=b.id;

更多推荐

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

发布评论

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

>www.elefans.com

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