gpt4 book ai didi

mysql - 从 id 不在另一个表中的表中选择

转载 作者:行者123 更新时间:2023-11-29 11:31:20 25 4
gpt4 key购买 nike

我想选择所有不属于某个公司的飞机。在本例中,我有三个表:PlanesCompaniesCompanyPlanes

这是我的查询:

SELECT *
FROM planes p,
companyplanes cp,
companies c
WHERE c.id = ?
AND cp.idCompany != c.id
AND (cp.idPlane = p.id OR p.id NOT IN (SELECT idPlane FROM companyplanes))
ORDER BY name ASC

但是这个查询没有返回任何结果!这里出了什么问题?

示例:

| Plane |
---------
id | name
---------
1 | p1
2 | p2
3 | p3


|Company|
---------
id | name
---------
1 | c1
2 | c2

| companyPlanes |
------------------------
id | idCompany | idPlane
------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2

如果我想获取不属于公司c2的飞机,结果应该是:p1,p3。

最佳答案

更新答案

我们可以通过以下方式得到结果

  1. 获取意外公司的所有飞机

    从 CompanyPlanes 中选择 idplane
    哪里 idCompany = ?

  2. 获取所有飞机,但不包括意外公司的飞机

    从飞机中选择 *
    身份证不在的地方
    (
    从 CompanyPlanes 中选择 idplane
    哪里 idCompany = ?
    )

您不需要与 Company加入,因为您已从 CompanyPlanes 表获取 idCompany

关于mysql - 从 id 不在另一个表中的表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37376563/

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