mysql两个表中的不同部分查询

tianwen_j 2016-12-11 10:26:49
表1 table_1
id
1
2
3
表2 table_2
id
1
2
3
4
5
6

-------
如何查询出table_2中跟table_1不同的部分,在线等,急急急!!谢谢
...全文
837 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 版主 2016-12-24
  • 打赏
  • 举报
回复
引用 3 楼 u012536120 的回复:
是用cmd.exe写的吗,还是别的?怎么复制出来的?
是 cmd.exe 写的; 右键->标记 ,开始选择,选择以后,在选中的区域,点一下右键,就复制成功了。
sanGuo_uu 2016-12-24
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

mysql> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)
............
是用cmd.exe写的吗,还是别的?怎么复制出来的?
  • 打赏
  • 举报
回复
一楼 已经给的很全面了,你也可以试试 差集 SELECT ID FROM T1 EXCEPT SELECT ID FROM T2
gzcitizeny 2016-12-22
  • 打赏
  • 举报
回复
select * from table_2 where id not in (select id from table_1)
卖水果的net 版主 2016-12-12
  • 打赏
  • 举报
回复

mysql> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> create table t2(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1), (2), (3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into t2 values(1), (2), (3), (4), (5), (6);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql>
mysql> -- 方法 1
mysql> select * from t2 where id not in(select id from t1) ;
+------+
| id   |
+------+
|    4 |
|    5 |
|    6 |
+------+
3 rows in set (0.00 sec)

mysql> -- 方法 2
mysql> select * from t2 where not exists(select * from t1 where t1.id = t2.id);
+------+
| id   |
+------+
|    4 |
|    5 |
|    6 |
+------+
3 rows in set (0.00 sec)

mysql> -- 方法 3
mysql> select t2.* from t2 left join t1 on t1.id = t2.id where t1.id is null;
+------+
| id   |
+------+
|    4 |
|    5 |
|    6 |
+------+
3 rows in set (0.00 sec)

mysql>
mysql> drop table t1,t2;
Query OK, 0 rows affected (0.00 sec)

mysql>

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧