sql 的交,并, 差集

本文介绍了SQL中的交集、并集和差集操作。通过示例展示了如何使用`UNION`、`INTERSECT`(可用JOIN替代)、`EXCEPT`(在MySQL中用`NOT IN`实现)来处理数据集合。同时,解释了`UNION ALL`会保留重复行,而`UNION`会去除重复行。对于交集,提供了联合查询和JOIN操作两种实现方式。此外,还探讨了不同类型的JOIN操作在无条件时产生的笛卡尔积效果。
摘要由CSDN通过智能技术生成

准备数据表

存在两张表websites 和 apps

create table websites (
    id int primary key auto increment,
    name varchar(255) not null,
    url varchar(255) not null,
    alexa int not null,
    country varchar(20) not null
) engine = innodb
  default charset = utf8mb4
  comment = '站点表';

create table apps (
    id int primary key auto increment,
    app_name varchar(255) not null,
    url varchar(255) not null, 
    country varchar(20) not null
) engine = innodb
  default charset = utf8mb4
  comment = "app表";

创建数据

insert into websites(id, name, url, alexa, country) values
(1, 'google', 'www.google.com', 1, 'usa'),
(2, '淘宝', 'www.taobao.com', 13, 'cn'),
(3, '菜鸟教程', 'www.runoob.com', 4689, 'cn'),
(4, '微博', 'www.weibo.com', 20, 'cn'),
(5, 'facebook', 'www.facebook.com', 3, 'usa'),
(6, 'stackoverflow', 'www.stackoverflow.com', 0, 'ind');


insert into apps(id, app_name, url, country) values
(1, 'qq app', 'www.qq.com', 'cn'),
(2, '微博 app', 'www.weibo.com', 'cn'),
(3, '淘宝 app', '
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值