mysql - 如何从两个表中获取未映射的行

标签 mysql sql sql-server

我在 SQL 查询中遇到问题

我有两个表:

表一

ID Number
1  100
2  200
3  300
4  400
5  500
6  600
7  700
8  800
9  900

表二

ID Someotherdata
1  100
2  200
3  300
4  400
5  500

我尝试了一个查询

Select one.ID,one.Number
from table1 one
Left Outer Join
table2 two
on one.ID=two.id

我要结果

6  600
7  700
8  800
9  900

最佳答案

使用 except :

Select one.ID,one.Number
from table1 one
except
(Select one.ID,one.Number
from table1 one
inner join
table2 two
on one.ID=two.id)

SQL Fiidle to test

编辑(根据 OP 要求)

SELECT table1.id, table1.number FROM table1
LEFT JOIN table2 on table2.id = table1.id
WHERE table2.id  IS NULL

Here is the SQL Fiidle for Testing

关于mysql - 如何从两个表中获取未映射的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007674/

相关文章:

mysql - 如何将 XML 转换为 SQL?

SQL查询行需要动态转换为列

sql-server - 如何从 Delphi 中执行包含许多 GO 语句的大型 SQL 脚本?

sql - 在 SQLite 中更新随机选择的行

sql-server - SQL Azure : How to execute a common DDL script across all schemas?

sql - 在更新到 SQL 时出现问号

mysql - 查找 SQL 查询 LIKE ('1077%' ) 运算符 ON DATA

MySQL varchar timestamp 列存储两种不同的方式

mysql - 显示MySQL中两列之间的所有关系

sql - mysql 查询没有给出正确的结果