掌握MySQL高级语言

1.导入数据库

mysql> source /backup/hellodb_innodb.sql;
#将脚本导入  source  加文件路径

2. select
显示表格中的一个或者多个字段中所有的信息
语法:
select 字段名 from 表名;

select * from info;
select name from info;
select name,id,age from info;

掌握MySQL高级语言

3. distinct
distinct 查询不重复记录
语法:
select distinct 字段 from 表名;

select distinct age from students;
#去除年龄字段中重复的

掌握MySQL高级语言

select distinct gender from students;
#查找性别

掌握MySQL高级语言

4. where
where 有条件的查询
语法:
select 字段 from 表名 where 条件 ​

select name,age from students where age < 20; 
#显示name和age 并且要找到age 小于20的

掌握MySQL高级语言

5.and;or
and 且 ; or 或
语法:
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;

select name,age from students where age<40 and age>20;
显示name和age,并且找到age大于20并且小于40的

掌握MySQL高级语言

select name,age,gender from students where age<40 or gender='m';
显示name,age,gender,并且age小于40或者gender为m

6.in
显示已知值的资料
#语法:
select 字段名 from 表名 where 字段 in (‘值1’,’值2’….);

#显示学号为1,2,3,4的学生记录
select * from students where StuID in (1,2,3,4);

掌握MySQL高级语言

#显示班级为1和3的学生记录
select * from students where ClassID in (1,3);

掌握MySQL高级语言

7.between
显示两个值范围内的资料
语法:
select 字段名 from 表名 where 字段 between ‘值1’ and ‘值2’;
包括 and两边的值

select * from students where name between 'ding dian' and 'ling chong';
# 一般不使用在字符串上
select * from students where stuid between 2 and 5;
#id 2到5 的信息 包括2和5
select * from students where age between '22' and '30';
#不需要表中一定有该字段,只会将22 到30 已有的都显示出来

掌握MySQL高级语言

8. like
通配符模糊查询通配符通常是和 like 一起使用
语法: select 字段名 from 表名 where 字段 like 模式 ​

select * from students where name like 's%';

掌握MySQL高级语言

select * from students where name like '%ong%';​

掌握MySQL高级语言

通配符 含义
% 表示零个,一个或者多个字符
_ 下划线表示单个字符
A_Z 所有以A开头 Z 结尾的字符串 ‘ABZ’ ‘ACZ’ ‘ACCCCZ’不在范围内 下划线只表示一个字符 AZ 包含a空格z
ABC% 所有以ABC开头的字符串 ABCD ABCABC
%CBA 所有以CBA结尾的字符串 WCBA CBACBA
%AN% 所有包含AN的字符串 los angeles
_AN% 所有 第二个字母为 A 第三个字母 为N 的字符串


9. order by

order by 按关键字排序
语法:
select 字段名 from 表名 where 条件 order by 字段 [asc,desc];
1. asc :正向排序
2. desc :反向排序
3. 默认是正向排序

#按学生的年龄正向排序显示年龄和姓名字段
 select age,name from students order by age;

掌握MySQL高级语言

#按学生的年龄反向排序显示年龄和姓名字段
select age,name from students order by age desc;

掌握MySQL高级语言

Intoep小程序

微信扫一扫,打开小程序浏览更便捷

转载作品,原作者:码薯,文章来源:https://blog.csdn.net/weixin_52588857/article/details/121677762

发表回复

登录后才能评论