后端基础SQL-高级查询与子查询(学习笔记)

一.MySql基础查询语句
1. order by 排序
select m* from a order by id;
以id字段进行排序
在这里插入图片描述
select * from a order by 2;
以第二个字段进行排序,这里只有两个字段只能:
order by 1
order by 2
如果order by 3超过3个字段就会报错,应为这里只有两个字段
在这里插入图片描述在这里插入图片描述desc倒序
select * from a order by id desc;
在这里插入图片描述2. limit n,m(分页)
n表示从第几行开始,m表示取第几条
select*from a limit 0,1
从第一条数据进行分页,取第一条数据

在这里插入图片描述两行数据
在这里插入图片描述四行数据
在这里插入图片描述select*from a limit 1,4;
从第二条数据开始,输出4条数据,因为是从第二条开始的所以只显示了 三条数据

在这里插入图片描述3.模糊查询
like(跟等于差不多)
select * from a where xm like ‘xiaoming’;
查询相似xiaoming的数据

在这里插入图片描述‘%%’ %号等于任意字符
select * from a where xm like ‘%x%’;
查询数据里面为x的数据,x%表示查询带有开头x的数据,%x表示查询结尾带有x的数据
在这里插入图片描述4.运算符号
(+ - * / %)
在这里插入图片描述5.逻辑运算
AND(&)与
OR (|)或
NOT (!)非
selectfrom a where xm=‘hack.hello’ and id=3;
查询xm为hack.hello的数据且id=3
要必须要满足两个条件
在这里插入图片描述 select
from a where xm=‘hack.hello’ or id =2;
查询xm字段带有hack.hello或id带2的数据
只要满足一个条件即可

在这里插入图片描述selectfrom a where not xm=‘hack.hello’
查询xm字段里面不为‘hack.hello’的数据
在这里插入图片描述6.sleep()延时
select sleep(5)
延时5秒回显
在这里插入图片描述二.联合查询
union将两个表里的数据合到一起输出
select
from a where id=3 union select 1,2;在这里插入图片描述三.子查询
select*from a where xm=(select ‘hack.hello’);
在这里插入图片描述

发布了14 篇原创文章 · 获赞 0 · 访问量 131

猜你喜欢

转载自blog.csdn.net/weixin_45291045/article/details/105024548