mysql数据库part3(查询进阶)

本文详细介绍了MySQL的查询语法,包括WHERE条件、GROUP BY、HAVING、ORDER BY、LIMIT和正则表达式查询。进一步探讨了多表查询中的内连接、外连接,并通过实例解析了子查询的应用,如找到平均年龄大于25岁的部门、查看特定部门员工、查找无员工的部门以及基于部门平均年龄的员工筛选。
摘要由CSDN通过智能技术生成

一、查询语法

sql查询语句基本语法:

select .. from .. where ..group by .. having .. order by .. limit..

1、where条件的使用

功能:对表中的数据进行过滤筛选

  1. 判断的符号:
    < > = <= >= != <>(不等于)

  2. 拼接条件的关键字:
    and or not

  3. 查询范围区间between
    between 小值 and 大值 [小值,大值] 查询两者之间的这个范围所有数据

  4. 查询某个值在具体某个范围里 in
    in(1,2,3,4)

  5. 模糊查询 like “%” “_” 通配符
    like “%a” 匹配以a结尾的任意长度字符串
    like “a%” 匹配以a开头的任意长度字符串
    like “%a%” 匹配含有a字母的任意长度字符串
    like “_a” 个数一共是两个字符,必须以a结尾,前面字符随意
    like “a__” 个数一共是三个字符,必须以a开头,后面字符随意

(1) 单条件查询
查询部门是sale的所有员工姓名:

select emp_name from employee where post="sale";

运行结果

(2) 多条件查询
部门是技术部,收入大于10000的所有人员及对应收入

select emp_name,salary from employee where post="技术部" and salary > 10000;

运行结果

(3) between … and …
收入在10000-20000之间的所有员工姓名和对应收入

select emp_name,salary from employee where salary between 10000 and 20000;

运行结果

收入不在10000-20000之间的所有员工姓名和对应收入
运行结果

(4) null关键字,在查询的时候,要使用is进行判断,不能用=

select * from employee where post_comment is null;

运行结果

(5) in 在…之中
查询收入是3000、4000、5000、8300所有员工的姓名和收入

select emp_name,salary from employee where salary in (3000,4000,5000,8300);

运行结果

not … in … 不在所列数据之中:

select emp_name,salary from emnployee where salary not in (3000,4000,5000,8300);

运行结果

写法二:

select emp_name,salary from employee where salary=3000 or salary=4000 or salary=5000 or salary=8300;

运行结果

(6) 模糊查询

“%”通配符

select emp_name,salary from employee where emp_name like "L%";

运行结果

“_”通配符

select emp_name,salary from employee where emp_name like "H__sir";

运行结果

(7) concat数据拼接(as 别名)

select concat("姓名:",emp_name,"收入:",salary) as info from employee;

运行结果

concat_ws(拼接的符号,参数1,参数2…):

select concat_ws(":",emp_name,salary) as info from employee;
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值