【问题标题】:Sids of supplier who supply some particular part using Nested Queries [duplicate]使用嵌套查询提供某些特定部件的供应商的 Sid [重复]
【发布时间】:2017-03-16 16:42:18
【问题描述】:

架构:

供应商(sid:整数,sname:字符串,地址:字符串)

零件(pid:整数,pname:字符串,颜色:字符串)

_Catalog(sid: integer, pid: integer, cost: real)

问题是:

查找供应红色零件或位于 221 的供应商的 sid 帕克大道。

我尝试了不同的方法,例如:

方法一:

select sid 
from   Suppliers 
where  sid = (select pid 
              from parts 
              where color= 'Red') 
or     address='221 Packer Ave';

方法二:

select sid 
from   _Catalog 
where  (pid IN(select pid from Parts where color='Red') 
        OR 
        sid IN(select sid from Suppliers where address='221 Packer Ave'));

第二个方法没有输出,第一个方法返回错误Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 我究竟做错了什么?还有其他解决方案吗?

注意:我需要一个嵌套查询来解决这个问题,因为我还没有研究过联接和推进的东西。

【问题讨论】:

  • 如果您使用的是 sqlserver .. 为什么要标记 mysql ???????
  • @scaisEdge 感谢您纠正我 :)

标签: sql


【解决方案1】:

刚刚想通了:)

select sid from Suppliers where address='221 Packer Ave' or sid IN (select sid from _Catalog where pid IN(select pid from Parts where color = 'Red'));

【讨论】:

    【解决方案2】:

    这是因为:

    select pid 
    from parts 
    where color= 'Red'
    

    返回多个pid,并且您在 where 条件下使用了=

    使用 IN 或将返回的 pid 限制为一行。

    select top (1) pid 
    from parts 
    where color= 'Red'
    

    【讨论】:

    • 供应商可以超过1个。
    【解决方案3】:

    好奇为什么不能只做内部连接?

    select parts.sid 
    from   Suppliers Suppliers inner join parts parts
    where  
    parts.color = 'Red' 
    or     
    parts.address = '221 Packer Ave'
    

    【讨论】:

    • 我只是个初学者,还没学过join:')
    【解决方案4】:

    如果您可以管理多个值,则可以使用 IN 子句

    select sid 
    from   Suppliers 
    where  sid IN   (select sid
                  from parts  p 
                  inner join _Catalog c c.pid= p.pid
                  where p.color= 'Red') 
    or     address='221 Packer Ave';
    

    您可以使用聚合函数,例如:max

    select sid 
    from   Suppliers 
    where  sid = (select max(sid)
                  from parts  p 
                  inner join _Catalog c c.pid= p.pid
                  where p.color= 'Red') 
    or     address='221 Packer Ave';
    

    或者您可以限制选择的行数或行数,例如:TOP 1

    【讨论】:

    • 答案错误,WHERE sid IN (SELECT pid...) 无效,供应商ID和产品ID无关...
    • @PhilP。谢谢...用关系回答更新
    【解决方案5】:

    解决问题的最佳方法是使用 JOIN 而不是派生查询。 这是一个例子:

    SELECT DISTINCT
        sup.sid
    FROM
        Suppliers AS sup
        LEFT OUTER JOIN _Catalog cat ON sup.sid = cat.sid
        INNER JOIN Parts par ON cat.pid = par.pid
    WHERE
        sup.address = '221 Packer Ave'
        OR cat.color = 'Red'
    

    您似乎缺少的是,要确定零件的供应商,您需要来自所有三个表的数据:零件有颜色,目录有供应商和零件之间的关系,最后供应商有 SID .

    另一种解决方案是 CTE,它有时可能性能更好(由于不同的查询计划,尤其是不需要 DISTINCT 的事实)但仅适用于 SQL Server 2008R2+:

    ;WITH CTE_RedPartSuppliers AS (
        SELECT
            cat.sid
        FROM
            Parts par
            INNER JOIN _Catalog cat ON par.pid = cat.pid
        WHERE
            par.color = 'Red'
    )
    
    SELECT
        sup.sid
    FROM
        Suppliers sup
    WHERE
        address = '221 Packer Ave'
        OR EXISTS (
            SELECT 1 FROM CTE_RedPartSuppliers rps WHERE rps.sid = sup.sid
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多