SQL子查询与加入混淆

时间:2022-05-30 23:33:39

I have a database which is in Access (you can get it link text). If I run

我有一个Access中的数据库(你可以得到它的链接文本)。如果我跑

SELECT DISTINCT Spl.Spl_No, Spl.Spl_Name
FROM Spl INNER JOIN Del 
   ON Spl.Spl_No = Del.Spl_No
WHERE Del.Item_Name <> 'Compass'

It provides the names of the suppliers that have never delivered a compass. However you can supposedly do this with a sub-query. So far myself and a few others have not been able to get it right.

它提供了从未提供指南针的供应商的名称。但是,您可以使用子查询来执行此操作。到目前为止,我和其他一些人都未能做到正确。

I did come close with the following, until we added more suppliers then it stopped working

我确实接近了以下内容,直到我们添加了更多供应商,然后它停止了工作

SELECT SPL.SPL_Name
FROM SPL
LEFT JOIN DEL ON Del.SPL_No = SPL.SPL_No
WHERE (DEL.Item_Name<>"Compass") OR (DEL.Item_Name IS NULL)
GROUP BY SPL.SPL_Name
HAVING COUNT(DEL.SPL_No) = 0

So the question: Is this possible to do with a sub-query.

所以问题是:这可能与子查询有关。

7 个解决方案

#1


Do you mean something like this?

你的意思是这样的吗?

SELECT SPL.SPL_Name
FROM SPL
WHERE NOT SPL.SPL_no IN 
 (SELECT SPL_no FROM DEL WHERE DEL.Item_Name = "Compass") 

#2


I think I would go for:

我想我会选择:

SELECT SELECT Spl_No, Spl_Name
FROM Spl
WHERE Spl_No NOT IN 
  (SELECT Spl_No FROM Del
   WHERE Item_Name = 'Compass')

#3


SELECT Spl_No
     , Spl_Name
  FROM Spl
 WHERE NOT EXISTS( SELECT *
                     FROM Del
                    WHERE Del.Spl_no = Spl.Spl_no
                      AND Item_name  = 'Compass' )

#4


If Access has Exists.....

如果Access存在.....

SELECT SPL.SPL_Name FROM SPL WHERE NOT EXISTS (SELECT 1 FROM DEL WHERE Del.SPL_No = SPL.SPL_No AND (DEL.Item_Name='Compass') )

SELECT SPL.SPL_Name FROM SPL WHERE NOT EXISTS(SELECT 1 FROM DEL WHERE Del.SPL_No = SPL.SPL_No AND(DEL.Item_Name ='Compass'))

#5


It's pretty close to exactly how you would say it in English

它非常接近你用英语怎么说

"Give me the suppliers who have not made a delivery of Compasses."

“给我那些没有交付指南针的供应商。”

Select [Stuff]
From Spl S
Where Not Exists
   (Select * From Del
    Where Spl_no = S.Spl_no 
       And Item_name  = 'Compass')

EDIT: Without Exists, you can use Count(*) = 0

编辑:没有存在,您可以使用Count(*)= 0

Select [Stuff]
From Spl S
Where 
   (Select Count(*) From Del
    Where Spl_no = S.Spl_no 
       And Item_name  = 'Compass') = 0

#6


Suppliers that have never delivered a compass:

从未交付指南针的供应商:

SELECT Spl.Spl_No, Spl.Spl_Name
FROM Spl
LEFT JOIN Del ON Del.Spl_No = Spl.Spl_No AND Del.Item_Name = 'Compass'
WHERE Del.Item_Name IS NULL

and using a sub query:

并使用子查询:

SELECT Spl_No, Spl_Name
FROM Spl
WHERE Spl_No IN 
  (
    SELECT Spl_No
    FROM Del
    GROUP BY Spl_No, Item_Name
    WHERE Item_Name = 'Compass'
    HAVING COUNT(*) = 0
  )

#7


Is this what you're looking for?

这是你在找什么?

SELECT
   SPL.SPL_name
FROM
   SPL
WHERE
   SPL.SPL_No NOT IN
   (
      SELECT SPL_No FROM DEL WHERE DEL.Item_Name <> "Compass"
   )

#1


Do you mean something like this?

你的意思是这样的吗?

SELECT SPL.SPL_Name
FROM SPL
WHERE NOT SPL.SPL_no IN 
 (SELECT SPL_no FROM DEL WHERE DEL.Item_Name = "Compass") 

#2


I think I would go for:

我想我会选择:

SELECT SELECT Spl_No, Spl_Name
FROM Spl
WHERE Spl_No NOT IN 
  (SELECT Spl_No FROM Del
   WHERE Item_Name = 'Compass')

#3


SELECT Spl_No
     , Spl_Name
  FROM Spl
 WHERE NOT EXISTS( SELECT *
                     FROM Del
                    WHERE Del.Spl_no = Spl.Spl_no
                      AND Item_name  = 'Compass' )

#4


If Access has Exists.....

如果Access存在.....

SELECT SPL.SPL_Name FROM SPL WHERE NOT EXISTS (SELECT 1 FROM DEL WHERE Del.SPL_No = SPL.SPL_No AND (DEL.Item_Name='Compass') )

SELECT SPL.SPL_Name FROM SPL WHERE NOT EXISTS(SELECT 1 FROM DEL WHERE Del.SPL_No = SPL.SPL_No AND(DEL.Item_Name ='Compass'))

#5


It's pretty close to exactly how you would say it in English

它非常接近你用英语怎么说

"Give me the suppliers who have not made a delivery of Compasses."

“给我那些没有交付指南针的供应商。”

Select [Stuff]
From Spl S
Where Not Exists
   (Select * From Del
    Where Spl_no = S.Spl_no 
       And Item_name  = 'Compass')

EDIT: Without Exists, you can use Count(*) = 0

编辑:没有存在,您可以使用Count(*)= 0

Select [Stuff]
From Spl S
Where 
   (Select Count(*) From Del
    Where Spl_no = S.Spl_no 
       And Item_name  = 'Compass') = 0

#6


Suppliers that have never delivered a compass:

从未交付指南针的供应商:

SELECT Spl.Spl_No, Spl.Spl_Name
FROM Spl
LEFT JOIN Del ON Del.Spl_No = Spl.Spl_No AND Del.Item_Name = 'Compass'
WHERE Del.Item_Name IS NULL

and using a sub query:

并使用子查询:

SELECT Spl_No, Spl_Name
FROM Spl
WHERE Spl_No IN 
  (
    SELECT Spl_No
    FROM Del
    GROUP BY Spl_No, Item_Name
    WHERE Item_Name = 'Compass'
    HAVING COUNT(*) = 0
  )

#7


Is this what you're looking for?

这是你在找什么?

SELECT
   SPL.SPL_name
FROM
   SPL
WHERE
   SPL.SPL_No NOT IN
   (
      SELECT SPL_No FROM DEL WHERE DEL.Item_Name <> "Compass"
   )