当前位置:网站首页>SQL练习题集合
SQL练习题集合
2022-07-17 05:09:00 【码赛客1024】
第一题:on后边跟and和where的区别
表结构如下:
CREATE TABLE `a` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `a` VALUES (1, 'a');
INSERT INTO `a` VALUES (2, 'b');
INSERT INTO `a` VALUES (3, 'c');
CREATE TABLE `b` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `b` VALUES (1, '甲');
INSERT INTO `b` VALUES (2, '乙');
SQL1:SELECT a.*,b.* FROM a LEFT JOIN b on a.id = b.id; 执行结果为:
+----+------+------+------+
| id | name | id | name |
+----+------+------+------+
| 1 | a | 1 | 甲 |
| 2 | b | 2 | 乙 |
| 3 | c | NULL | NULL |
+----+------+------+------+
SQL2:SELECT a.*,b.* FROM a LEFT JOIN b ON a.id = b.id AND b.id=2;执行结果为:
+----+------+------+------+
| id | name | id | name |
+----+------+------+------+
| 1 | a | NULL | NULL |
| 2 | b | 2 | 乙 |
| 3 | c | NULL | NULL |
+----+------+------+------+
SQL3:SELECT a.*,b.* FROM a LEFT JOIN b ON a.id = b.id WHERE b.id=2;执行结果为:
+----+------+------+------+
| id | name | id | name |
+----+------+------+------+
| 2 | b | 2 | 乙 |
+----+------+------+------+
小结:
1.on后面条件用and,不管条件是否成立都会把左表的数据全部展示。
2.on后面条件用where,在left join 生成的表基础上在做筛选,这时会把where中不成立的筛选掉。
第二题:…
边栏推荐
- Teach you to reproduce log4j2 nuclear weapon level vulnerability hand in hand
- typedef
- Redis source code analysis dynamic string implementation (SDS)
- Face scum counter attack: thread pool lethal serial eighteen questions, the interviewer praised me straight
- 1.东软跨境电商数仓需求规格说明文档
- 【Bug解决】org.apache.ibatis.type.TypeException: The alias ‘xxxx‘ is already mapped to the value ‘xxx‘
- 递归的应用
- Common methods of goframe error handling & use of error codes
- Excel imports long data and changes to 000 at the end
- Application of recursion
猜你喜欢

3.东软跨境电商数仓项目架构设计

9.数据仓库搭建之DIM层搭建

Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation

Cityengine 3D pipe modeling tutorial

C语言&位域

mysql的锁

Wechat applet learning notes

The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan

MySQL学习笔记(4)——(基本CRUD)操作数据库中的表的数据

Excel calculates the remaining days of the month
随机推荐
The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan
Three methods for cesium to obtain the longitude and latitude at the mouse click
Round robin schedule problem
MySQL cache solution problem solving
BUUCTF web WarmUp
Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation
Parent components plus scoped sometimes affect child components
Redis source code analysis dynamic string implementation (SDS)
sql时间对比
GoFrame 错误处理的常用方法&错误码的使用
对象转map
【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从
ArcGIS point cloud (XYZ) data to DEM
11.数据仓库搭建之DWS层搭建
C语言动态内存管理
时间差计算
mysql的使用
Shell script configures root to login to other hosts without secret
12.数据仓库搭建之ADS层搭建
解决idea新建module 提示module xxxx does exitst