[英]How can I select items from one sql table that don't appear in another table
I have a Customer table which has an ID. 我有一个具有ID的客户表。 Each Customer entry has a Design which is stored in a Design table (it contains the CustomerID to reference). 每个客户条目都有一个设计,该设计存储在设计表中(其中包含要引用的客户ID)。
In my scenario, a Customer can have several Designs and sometimes no Designs. 在我的情况下,客户可以有多个设计,有时没有。 How could I select Customers that only have Designs? 如何选择仅具有外观设计的客户?
I've tried doing an Inner Join like this but I still get too many records since a Customer can have many Designs: 我已经尝试过像这样进行内部联接,但是由于客户可以有很多设计,因此我仍然获得太多记录:
Select * from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
Where Design.CustomerID is not null
*
select all records of all tables. *
选择所有表的所有记录。 Use tablename.*
to select only all records of a specific table. 使用tablename.*
仅选择特定表的所有记录。
Select Customer.*
from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
But actually you are always better off by explicitly defining which columns you need. 但是实际上,通过明确定义所需的列总是可以使您更好。 So use 所以用
Select Customer.ID, Customer.Col2, Customer.Col3
from Customer
Inner Join Design On Design.CustomerID = Customer.ID
group by Select Customer.ID, Customer.Col2, Customer.Col3
And when you use an inner join
then only the records will be returned that actually have a link to the joined table - so your where
clause is obsolete. 而且,当您使用inner join
联接时,将仅返回实际上具有联接表链接的记录-因此,您的where
子句已过时。
i guess you storing an empty strings in your database . 我想你在数据库中存储了一个空字符串。
try that 试试看
Select * from Customer
Inner Join Design
On Design.CustomerID = Customer.ID
Where Design.CustomerID is not null
AND Design.CustomerID != ''
GROUP BY Customer.ID
You can do a where exists
你可以做一个where exists
的where exists
select * from Customer
where exists (select 1 from Design where CustomerId = Customer.ID)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.