加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

Mysql查询语句汇总

发布时间:2022-11-06 14:32:03 所属栏目:MySql教程 来源:网络
导读: 一、单表查询 1、全表数据查询
语法格式:select * from 表名;
例:select * from students;
2、查询指定字段
语法格式:select 字段1,字段2 from 表名;
例:select name,age from student

一、单表查询 1、全表数据查询

语法格式:select * from 表名;

例:select * from students;

2、查询指定字段

语法格式:select 字段1,字段2 from 表名;

例:select name,age from students;

3、带where表达式的查询

语法格式:select 字段名 from 表名 where 条件表达式;

例:select name from students where score = 98;

select name from students where score>=80;

4、带in关键字查询

语法格式:select 字段名 from 表名 where 字段 [not]in(元素1,元素2);

例:select name from students where score in(60,70);

5、带between and的范围查询

语法格式:select 字段名 from 表名 where 字段 [not]between 取值 and 取值1;

例:select name from student where age between 20 and 25;

6、带like的模糊查询

'%'代表任意字符;

'_'代表单个字符;

select 字段名 from 表名 where 字段名 like’字符串’;

例:select score from student where name like’%张%';匹配含有张的所有字符

mysql 拼音查询_mysql查询_mysql查询日志

7、空值查询

select 字段1,字段2… from students where 字段 is [not] null

8、带and、or的多条件查询

语法格式:select 字段1,字段2 from 表名 where 条件1 and 条件2;

select 字段1,字段2 from 表名 where 条件1 or 条件2;

例:select name score from students where grade=‘计算机’ and age=24;

select name score from students where grade=‘计算机’ or age=24;满足一个条件执行

9、去重查询(distinct)

语法格式:select distinct 字段名 from 表名;

10、对查询结果排序

语法格式:select 字段1,字段2… from students order by 属性名 [desc|asc];

例:select * from student order by age desc;

注:desc从大到小排序,asc从小到大排序(默认排序方式)

11、分组查询(group by)

语法格式:group by 属性名 [having 条件表达式][with rollup]

1、与group_concat()函数一起使用

select class,GROUP_CONCAT(name) from students.users GROUP BY class;

image.png

2、与聚合函数一起使用

select class,COUNT(name) from students.users GROUP BY class;

image.png

12、limit分页查询

语法格式:select * from 表名 limit 起始值,记录数;

select * from students.users limit 2,7;

二、多表查询 1、左连接(LEFT JOIN)

select 字段列表

from A表 left join B表

on 关联条件

例:select stu_users.sno,stu_users.sname,sc.score from stu_users

LEFT JOIN sc on stu_users.sno=sc.sno;

2、右链接

select 字段列表

from A表 right join B表

on 关联条件

3、联合查询

select 语句1

union[union 选项]

select 语句2

union|[union 选项]

select 语句n

注:其中union选项有两个选项可选

all:表示无论重复都输出

distinct: 去重(整个重复)(默认的)

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!