MySQL查询语句(1)

连接数据库

mysql -hlocalhost -uroot -proot

DQL-介绍

DQL英文全称是Data Query Language(数据查询语言),数据查询语言,用来查询数据库中表的记录

查询

SELECT
  字段列表
FROM
  表名列表
WHERE
  条件列表
GROUP BY
  分组字段列表
HAVING
  分组后条件列表
ORDER BY
  排序字段列表
LIMIT
  分页参数

查询一个表中的多个字段

select 字段1,字段2,.... from 表名;
如: select id, workno,name,sex,age,intime from person;
或者
select * from 表名; [这一条语句实际上使用的很少,影响效率]

查询【别名】

select 字段名1 [as 别名1], 字段名2 [as 别名2],....... from  表名;
as可以省略的。
如: select  id '用户id' from person;

去除重复的记录 select distinct

select distinct 字段列表 from 表名;
如: select distinct intime from person;

条件查询

条件查询语法:select 字段1,字段2,字段3,字段4,.. from 表明 where 条件列表;
条件列表可以是如下:(1)比较运算符 (2)
### 比较运算符。 
1.0) >  >=   <   <=   <>或者!=
1)between ...and... (ps:它既包含最大值也包含最小值)
2)in(...)  多个值中满足其中一个就行
3)like模糊匹配, 
4)is nulll 表示存储的是null
#### 逻辑运算符:
1) and 或者 &&
2) or 或者 ||
3) not 或者 !

查询名称是张三

select * from peron where name ='张三'

查询年龄小于30的人

select * from peron where age < 30

年龄不等于20的员工

select * from peron where age <> 20
或者
select * from peron where age != 20

查询没有身份证证号的员工

select * from peron where idcard is null; [没有身份证存储的是null,所以查询的是is null]

查询有身份证的员工

select * from peron where idcard is not null; [is not null表示存储的不是null]

查询入职时间是在 2019年(包含)到2023年(包含)之间的人

select * from peron where intime >= 2019 && intime <= 2023
或者 
select * from peron where intime >= 2019 and intime <= 2023
或者
select * from peron where intime bttween 2019 and 2023;
// bttween ... and...它既包含最大值也包含最小值,
bttween 后面跟的是最小值, and 后面跟的是最大的值

查询年龄是 18或20或30

select * from peron where age = 18 or age = 20 or age = 30;
或者下面这样使用 in 
select * from peron where age in(18,20,30);
ps: age in(18,20,30) 年龄是 18或20或30

查询姓名是两个字符的员工信息

select * from peron where name like '__';
ps:一个下划线表示一个字符。

查询身份证最后一位是X的员工

select * from peron where idcard like '%X';
ps:%以任意开头。 %X以任意开头,以X结尾

尾声

之前一直在说卷后端,都没有怎么行动,现在开始行动起来了。
现在的计划使用60天的时间,去学习MySQL。
希望学完之后,可以做到熟练使用。
加油! 学习的第四天。
posted @ 2023-08-03 22:56  南风晚来晚相识  阅读(11)  评论(0编辑  收藏  举报