简体   繁体   English

sql-从一个表中选择另一个表中不存在的记录

[英]sql - Select records from one table that do not exist in the other

I am trying to select all the records from the table "users" that do not exists in the table "clubs". 我试图从表“用户”中选择表“ clubs”中不存在的所有记录。 However, the sentence is still showing some records that exist in the table "clubs". 但是,该句子仍显示表“ clubs”中存在的一些记录。

Table: Users 表:用户

ID, Name, Registration_Date
***************************
3, Mark, 5/1/2017
7, Paul, 5/2/2017
8, John, 5/3/2017

Table: Clubs 表:俱乐部

************
User_ID, Club_Name
  3, Chicago
  3, Texas
  7, Miami
  7, San Antonio

This is the SQL sentence: 这是SQL语句:


SELECT 
    [Users].[ID], 
    [Users].[Name]
FROM 
    [Users]
LEFT JOIN 
    [Clubs]
ON 
    [Users].[ID] = [Clubs].[User_ID]  
WHERE
    [Clubs].[User_ID] IS NULL AND
    [Users].[Status] = 'Active' AND
    [Users].[Gender] = 'Female'
GROUP BY 
    [Users].[ID],
    [Users].[Name]
ORDER BY 
    [Users].[ID] DESC

This is the result that I am getting 这是我得到的结果


8, John
3, Mark

For some reason, the user with ID 3 is still showing, which exists in the "clubs" table. 由于某些原因,ID为3的用户仍在显示,该用户存在于“俱乐部”表中。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

It reads like English: Select from User where User.Id is not in Clubs. 它看起来像英语:从User中选择User.Id不在Clubs中。 Here is the query: 这是查询:

SELECT 
    [Users].[ID], 
    [Users].[Name]
FROM 
    [Users]
WHERE
    [Users].[Status] = 'Active' AND
    [Users].[Gender] = 'Female' AND 
    [Users].[ID] not in (select UserID from Clubs)
ORDER BY 
    [Users].[ID] DESC

More info about the IN operator . 有关IN运算符的更多信息。

This was the resolution to the problem provided by James: 这是詹姆斯提供的问题的解决方案:

Check if there is an extra space (or something like that) in either table for the id field for the "3" records, which may cause it not to match correctly. 检查任一表中是否有多余的空间(或类似的东西)用于“ 3”记录的id字段,这可能导致其不正确匹配。 I've recreated that DB locally and the query worked for me. 我已经在本地重新创建了该数据库,该查询对我有用。 -

The problem was a space in the column Status. 问题是“状态”列中有空格。

I added this comment as the right answer was provided as a comment, and did not find any other option to accept the comment as the right answer. 我添加了此评论,因为提供了正确答案作为评论,但没有找到其他任何选择来接受该评论作为正确答案。

Try this 尝试这个

SELECT 
    [Users].[ID], 
    [Users].[Name]
FROM 
    [Users]
WHERE
    [Users].[ID] NOT IN (SELECT [Clubs].[User_ID] FROM [Clubs]) AND
    [Users].[Status] = 'Active' AND
    [Users].[Gender] = 'Female'
GROUP BY 
    [Users].[ID],
    [Users].[Name]
ORDER BY 
    [Users].[ID] DESC

Updated Query 更新查询

SELECT 
[Users].[ID], 
[Users].[Name]
FROM 
[Users]
WHERE
[Users].[ID] IN 
(SELECT [Users].[ID] except (SELECT UserId from Clubs group by Clubs.UserId)) 
AND
[Users].[Gender] = 'Female' AND
[Users].[Status] = 'Active' // other filters as necessary
ORDER BY 
[Users].[ID] DESC

May need to do an execution plan, may not be perfomant, but gives you the necessary result 可能需要制定执行计划,可能没有执行力,但是会为您提供必要的结果

  SELECT [Users].[ID], [Users].[Name] 
  FROM [Users]  
  WHERE not exists(Select 1 from [Clubs] where [Clubs].[User_ID]=[Users].[ID]) 
  AND [Users].[Status] = 'Active' 
  AND [Users].[Gender] = 'Female' 
  GROUP BY [Users].[ID], [Users].[Name] 
  ORDER BY [Users].[ID] DESC

User ID not exists in the Club table and do exists in the Users table will be fetched. Club表中不存在用户ID,而Users表中确实存在用户ID。

I think the problem is the left join. 我认为问题是左联接。

Change this: FROM [Users] LEFT JOIN [Clubs] 更改此设置:从[用户]左加入[俱乐部]

For this: FROM [Users] RIGHT JOIN [Clubs] 为此:从[用户]右加入[俱乐部]

暂无
暂无

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

相关问题 如果存在 2 条记录,则 SQL 从表中选择 - SQL select from a table if 2 records exist Select 一个表中的所有记录和另一个表中的匹配记录或 Null(如果不存在) - Select All Records From One Table And Matching Record From Other Table or Null (if not exist) 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 如何从一个表中选择另一张表中不存在的所有记录? - How to select all records from one table that do not exist in another table? 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table? 如何从一个表中选择特定年份不存在的所有记录? - How to select all records from one table that do not exist in particular year? 如何显示一个表中不存在于另一表中的记录 - How to show records from one table that do not exist in another table SQL - 从一个表中查找存在于另一个表中的记录,如果不存在则删除 - SQL - find records from one table which exist in another and delete if not PHP / SQL:如何从一个表中选择完全不同的表中不存在的值 - PHP/SQL: How to SELECT values from one table that do not exist in a completely different table SQL从一个或另一个表中选择 - SQL select from either one or other table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM