我的SQL查询有什么问题?

时间:2017-05-15 11:21:07

标签: mysql sql

我正在尝试将这些查询实施到我的sql,但是我似乎无法找到我做错的事情。有人可以帮忙吗?

Application(Id, Name, ShortName)
Uses(ApplicationId, ModuleId)
Module(Id, Name, LinesOfCode, Status)
# The status of a module can be alpha or beta.
# An alpha module is usually an early release with more
# risk that there is an error than for a beta release.
Function(Id, Name, ModuleId, LinesOfCode)

列出所有不使用状态为alpha的模块的应用程序。

我的查询:

    SELECT * 
    FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
    WHERE ModuleId IS NOT (Select Module.Id FROM Module WHERE Status = "Alpha")

列出使用状态为alpha的模块和状态为beta的模块的所有应用程序。

我的查询:

    SELECT * 
    FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
    WHERE ModuleId = (SELECT Module.Id FROM Module WHERE Status = "Beta") 
          AND ModuleId = (SELECT Module.Id FROM Module WHERE Status = "Alpha");

3 个答案:

答案 0 :(得分:0)

你可以 NOT IN MySQL中没有 IS NOT 功能

SELECT * 
        FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
        WHERE ModuleId  NOT IN (Select Module.Id FROM Module WHERE Status = "Alpha")

和 看到您的此查询,您可能会获得多个ID,因此您应该使用IN代替=

SELECT * 
    FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
    WHERE ModuleId IN (SELECT Module.Id FROM Module WHERE Status = "Beta") 
          AND ModuleId IN (SELECT Module.Id FROM Module WHERE Status = "Alpha");

答案 1 :(得分:0)

试试这个。

SELECT * 
FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
WHERE ModuleId NOT IN (Select Module.Id FROM Module WHERE Status = "Alpha")

SELECT * 
FROM Application INNER JOIN Uses ON Application.Id = ApplicationId 
WHERE ModuleId IN (SELECT Module.Id FROM Module WHERE Status = "Beta") 
      AND ModuleId IN  (SELECT Module.Id FROM Module WHERE Status = "Alpha");

答案 2 :(得分:0)


使用以下查询。

SELECT * 
FROM Application A 
INNER JOIN Uses U ON (A.Id = U.ApplicationId)
INNER JOIN Module M ON (A.Id = M.Id)
WHERE M.Status  IN ('Beta','Alpha');

它提供所有列。您可以使用别名并拉出所需的列