except在mysql中怎么实现_有关PGSQL的INTERSECT和EXCEPT以及在MYSQL中的实现代码

有关PGSQL的INTERSECT和EXCEPT以及在MYSQL中的实现。

一下是表语句。

create table t1 (id serial not null,game_id int not null);

insert into t1(game_id) values (1),(2),(3),(5),(6),(8);

create table t2 (id serial not null,game_id int not null);

insert into t2(game_id) values (1),(2),(6),(8),(10),(4);

PGSQL:

交集,

t_girl=# select game_id from t1 intersect select game_id from t2;

game_id

---------

1

2

6

8

(4 rows)

Time: 0.400 ms

t_girl=# select game_id from t1 where game_id not in (select t1.game_id from t1 left join t2 on t1.game_id = t2.game_id where t2.game_id is null);

game_id

---------

1

2

6

8

(4 rows)

Time: 0.530 ms

t_girl=#

差集,

t_girl=# select game_id from t1 except select game_id from t2;

game_id

---------

3

5

(2 rows)

Time: 0.371 ms

t_girl=# select t1.game_id from t1 left join t2 using(game_id) where t2.game_id is null;

game_id

---------

3

5

(2 rows)

Time: 0.471 ms

MYSQL没有提供这两个关键字,可以用LEFT JOIN来代替。

交集,

mysql> select game_id from t1 where game_id not in (select t1.game_id from t1 left join t2 on t1.game_id = t2.game_id where t2.game_id is null);

+---------+

| game_id |

+---------+

| 1 |

| 2 |

| 6 |

| 8 |

+---------+

4 rows in set (0.01 sec)

mysql>

差集,

mysql> select t1.game_id from t1 left join t2 using(game_id) where t2.game_id is null;

+---------+

| game_id |

+---------+

| 3 |

| 5 |

+---------+

2 rows in set (0.00 sec)

以后的再更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值