MySQL Join 和内部选择

发布于 2024-10-04 03:30:45 字数 284 浏览 3 评论 0原文

我目前有一个疑问

SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE 条件 = 0)

Table1 与 table2 具有 1 - Many 关系,我正在查找 table2 中没有条件条目的所有 ID =0。

有没有办法在没有内部选择的情况下重写这个查询?我已经为此摸不着头脑有一段时间了,欢迎任何指点。

I currently have a query

SELECT id FROM table1 WHERE {filters on table1} AND id NOT IN (SELECT table1ID FROM table2 WHERE condition = 0)

Table1 has a 1 - Many relationship with table2 and I'm looking for all the IDs that have no entries in table2 with condition=0.

Is there any way to rewrite this query without the inner select? I'm been scratching my head about it for a while now and any pointers would be welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2024-10-11 03:30:45

你可以尝试类似的东西

SELECT  id 
FROM    table1 t1 LEFT JOIN
        table2 t2   ON  t1.ID = t2.table1ID
                    AND t2.Condition = 0
WHERE   {filters on table1} 
AND     t2.table1ID IS NULL

或者 一样好

SELECT  id
FROM    table1 t1
WHERE   {filters on table1} 
AND     NOT EXISTS  (
                        SELECT  1
                        FROM    table2 t2
                        WHERE   t1.ID = t2.table1ID
                        ADN     t2.condition = 0
                    )

You can try something like

SELECT  id 
FROM    table1 t1 LEFT JOIN
        table2 t2   ON  t1.ID = t2.table1ID
                    AND t2.Condition = 0
WHERE   {filters on table1} 
AND     t2.table1ID IS NULL

Or just as good would be

SELECT  id
FROM    table1 t1
WHERE   {filters on table1} 
AND     NOT EXISTS  (
                        SELECT  1
                        FROM    table2 t2
                        WHERE   t1.ID = t2.table1ID
                        ADN     t2.condition = 0
                    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文