sql语句复习

简单基础

sql语句去重

image-20220602201107144

查询出所有大学

1
2
3
select distinct university from user_profile

select university from user_profile group by university

查询出某个范围的数据

image-20220602202431393

1
2
3
select device_id,gender,age from user_profile where age>=20 and age<=23

select device_id,gender,age from user_profile where age between 20 and 23

当然也可以使用not between来进行操作

排除列查询

image-20220602203024174

1
2
3
select device_id,gender,age,university from user_profile where university != '复旦大学'

select device_id,gender,age,university from user_profile where university not in ('复旦大学')

其他

数据内连接与外连接

MySQL—内连接和外连接区别_leon.han的博客-CSDN博客_mysql内连接和外连接的区别

内连接:取出按照条件的数据,匹配不到的不进行保留

1
select * from users as u inner join topics as t on u.id=t.user_id

外连接:取出连接表中匹配到的数据,匹配不到的也会保留,值为null

1
2
3
4
#左边的表为主
select * from users as u left join topics as t on u.id=t.user_id;
#右边的表为主
select * from topics as t right join users as u on u.id=t.user_id;

当然还有全外链接