简体   繁体   English

SQL查询从一个表中选择不在另一表中的行

[英]SQL query to select row from one table which is not in another table

Registraion and group_members Registraiongroup_members

Registration Table
id  name  
-------                
1   A    
2   B    
3   C
4   D

group_members Table
name  Gid 
-------                
A   01    
B   01    
C   02

I need to get the names from registration table which are not a member in group_members table with Gid is 02 . 我需要从注册表中获取名称,该名称不是Gid 为02的 group_members表中的成员。 The output must be obviously A,B and D . 输出必须明显是A,B和D。 But I dont know how to achieve this.Please help.Thanks 但是我不知道该怎么实现,请帮忙。

Try this: 尝试这个:

SELECT t1.*
FROM Registration AS t1
LEFT JOIN Group_members AS t2 ON t1.name = t2.name AND t2.Gid = '02'
WHERE t2.name IS NULL 

This will filter out any matching records in group_members table with Gid = '02' . 这将滤除group_members表中group_members Gid = '02'所有匹配记录。

this should work 这应该工作

select name from
Registration reg where 
not exists 
(select null 
from group_members gm
where gm.name = reg.name
and gm.gid = '02')

暂无
暂无

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

相关问题 sql select查询一个表中的一行,而另一表中的多行 - sql select query for one row in one table and multiple row from another table SQL query INSERT SELECT COUNT 从一个表逐行到另一个表 - SQL query INSERT SELECT COUNT from one table to another row by row SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another SQL查询从一个表中选择,其中不在另一个表中或在该表中具有特定值 - SQL query to select from one table where either not in another table OR in that table with a specific value 如何从表中选择数据,该数据是另一个表中的行数据 - how to select data from table which is a row data in another table sql将行从一个表移动到另一个表 - sql move row from one table into another SQL查询以从一个表中选择一个值,从另一表中选择另一个值,并显示计数 - SQL query to select one value form one table and other from another table and also display count Sql 查询 select 来自另一个表的几个项目与表的单行上的值相关 - Sql query to select several items from a another table in relation to values on a single row of a table SQL如果在一个表中不存在,则从另一表中选择/插入行 - SQL Select/insert a row from another table if it doesn't exist in one table SQL从一个表中选择不在另一个表中的项 - SQL select item from one table that is not in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM