MySQL8.0基础操作2
1. 联合操作
使用union关键字,将users表与users2表联合起来(去重):
select * from users
union
select * from users2;
使用多个union可以将多个表联合起来(去重):
select * from users2
union
select * from users
union
select * fro users3;
使用多个union all可将多个表联合起来,并且不去重:
select * from users3
union all
select * from users
union all
select * from users2;
2. 连接操作
表users与表users2的信息如下:
1、内连接,使用join或inner join,表示求出两个表的交集,即在笛卡尔集中挑出两个表中使on成立的记录:
select * from users
join users2 on users2.userID=users.userID;
2、左连接,使用left join,表示求两个表的交集加上左表的剩余记录,即在笛卡尔集中挑出两个表中使on成立的记录,并加上左表中剩余的记录:
select * from users
left join users2 on users2.userID=users.userID;
3、右连接,使用right join,表示求两个表的交集加上右表的剩余记录,即在笛卡尔集中挑出两个表中使on成立的记录,并加上右表中剩余的记录:
select * from users
right join users2 on users2.userID=users.userID;
3. 排序
将表users按照Fee从小到大排序,因为属性Fee定义的是字符型,故从最高位开始,按照ASCII比较,这里因为9比1的ASCII大,所以排序结果如下:(如果把Fee定义为Int型,则可以按照数值大小排序)
select * from users
order by Fee;
将表users按照Fee从大到小排序:
select * from users
order by Fee desc;
4. 正则表达式
使用regexp关键字(regular expression)
1、表示在users表中查询userName以a开头的记录:
selec * from users where userName regexp'^a';
2、表示在users表中查询userName以b结尾的记录:
select * from users where userName regexp'b$';
3、表示在users表中查询userName中含b的记录:
select * from users where userName regexp'b';
4、表示在users表中查询userName中含0个及以上b的记录:
select * from users where userName regexp'b*';
4、表示在users表中查询userName中含1个及以上b的记录:
select * from users where userName regexp'b+';
5. 临时表
使用temporary关键字创建临时表:
1、使用show tables查看数据库中的表示,无法查看到临时表,但是临时表与普通表一样,可以进行一系列操作。
2、使用完临时表后可以使用drop删除。
3、在使用exit退出mysql操作界面,再进去之后,之前创建的临时表会消失。
create temporary table users4(userID varchar(10),userName varchar(10));
6. 查询属性是否为空
表users的信息如下:
在users表查询出userS为null的记录:
select * from users where userS is null;
在users表查询出userS不为null的记录
select * from users where userS is not null;