简体   繁体   English

如何从一个SQL表中选择未出现在另一表中的项目

[英]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 existswhere exists

select * from Customer
where exists (select 1 from Design where CustomerId = Customer.ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中选择另一个表中不存在的行? - How can I select rows from a table that don't exist in another table? 如何将行从一个表移动到另一个表中不存在的行? - How can I move rows from one table to another where they don't exist in a third? SQL从一个表中选择项目,从另一个表中选择条件 - SQL select items from one table with conditions from another 选择另一个表中没有匹配项的项目 - Select Items that don't have matching Items in another table 我如何在sql中限制,如果选择了一个表行,则仅显示另一表中的相应项目? - How do i limit in sql, that if one table row is selected, only corresponding items from another table show? 如果另一个值不存在,如何根据一个值从表中选择项目? (口才/ SQL) - How to select items from a table based on one value if another value does not exists? (eloquent/sql) MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 从没有在另一个表中出现的表中选择PHP - Select from table where doesn't appear in another table PHP 如何在一个SQL查询中选择同一表中的相关项目 - How can I select related items within the same table in one SQL query 如何根据一个表中的数据选择一个表中的数据 - How can I select data from one table depending on the data from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM