当前位置:网站首页>MySQL之CRUD
MySQL之CRUD
2022-08-11 05:35:00 【雨沐笙】
目录
一、基础查询
1)单个字段查询&多个字段查询&查询所有字段
改三个查询语句都相同,就是除了select后面的字段名字不一样而已
①、单个字段查询
语法:
select employee_id from t_mysql_employees
效果:

②、多个字段查询
语法:
select employee_id,first_name from t_mysql_employees
效果:
③、查询所有字段
语法:
select * from t_mysql_employees
效果:
2)查询常量值&表达式&函数
②、查询常量
语法:
SELECT 100;
SELECT 'john';效果:


③、表达式
语法:
SELECT 100%98;
效果:

④、函数
语法:
SELECT VERSION();效果:

改函数是查询数据库的版本
3)起别名&去重
①、起别名
第一种方式 as语法:
select employee_id as 员工编号 from t_mysql_employees
第二种方式 空格语法:
select employee_id 员工编号 from t_mysql_employees
两种的效果都是相同的,如以下图

②、去重

语法:
select DISTINCT manager_id from t_mysql_employees
效果:

4)案例:+号的作用
语法:
select 'liuliu'+1效果:

java中的+号:
①运算符,两个操作数都为数值型
②连接符,只要有一个操作数为字符串
mysql中的+号:
仅仅只有一个功能:运算符
二、过滤查询
1)按条件表达式筛选
示例:查询部门编号不等于90号的员工名和部门编号
语法1:
select last_name,department_id from t_mysql_employees where department_id <> 90语法2:
select last_name,department_id from t_mysql_employees where not(department_id = 90)
效果:
2)按逻辑表达式筛选
示例:查询工资z在10000到20000之间的员工名、工资以及奖金
语法:
select * from t_mysql_employees where salary BETWEEN 10000 and 20000
效果:

三、模糊查询
1)like
示例1:查询员工名中包含字符a的员工信息
语法:
select * from t_mysql_employees where last_name like '%a%'
效果:
示例2:查询员工名中第二个字符为_的员工名
语法:
select * from t_mysql_employees where last_name like '_$_%' escape '$'
效果:

因为_是占位符,所有我们要用$来替代
示例3:查询员工名中第二个字符为e,第五个字符为a的员工名和工资
语法:
select * from t_mysql_employees where last_name like '__e_a%'
效果:
2)between and
示例:查询员工编号在100到120之间的员工信息
语法:
select * from t_mysql_employees where employee_id BETWEEN 100 and 200
效果:
3)in
示例:查询员工的工种编号是 IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号
语法:
select * from t_mysql_employees where job_id in('IT_PROG','AD_VP','AD_PRES')
效果:
4)is null
示例1:查询没有奖金的员工名和奖金率
语法:
select first_name,commission_pct from t_mysql_employees where commission_pct is null
效果:
示例2:查询有奖金的员工名和奖金率
语法:
select first_name,commission_pct from t_mysql_employees where commission_pct is not null
效果:

示例3:查询没有奖金的员工名和奖金率
语法:
select first_name,commission_pct from t_mysql_employees where commission_pct <=> null
效果:
#is null pk <=>
IS NULL:仅仅可以判断NULL值,可读性较高,建议使用
<=> :既可以判断NULL值,又可以判断普通的数值,可读性较低
四、order by 子句
1)、按单个字段排序
#案例:按员工表薪资排序
select * from t_mysql_employees ORDER BY salary结果

2)、添加筛选条件再排序
#案例:查询部门编号>=90的员工信息,并按员工编号降序
select * from t_mysql_employees where department_id >=90 ORDER BY employee_id desc;结果
3)、按表达式排序
案例:查询员工信息 按年薪降序
select em.salary*12*(1+IFNULL(commission_pct,0)),em.* from t_mysql_employees em ORDER BY em.salary*12*(1+IFNULL(commission_pct,0)) desc; 结果
4)、按函数排序
案例:查询员工名,并且按名字的长度降序
select CONCAT(first_name,last_name),LENGTH(CONCAT(first_name,last_name)) from t_mysql_employees ORDER BY LENGTH(CONCAT(first_name,last_name)) desc结果
5)、按多个字段排序
#案例:查询员工信息,要求先按工资降序,再按employee_id升序
select * from t_mysql_employees order by salary desc,employee_id ASC;结果

边栏推荐
猜你喜欢

HCIP BGP建邻实验

CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)

CLUSTER DAY01(集群及LVS简介 、 LVS-NAT集群 、 LVS-DR集群)

Top20 bracket matching

AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)

lvm multi-disk mount, combined use

八股文之redis

会议OA项目之我的会议

Slurm基本使用

SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
随机推荐
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-26
HCIP-BGP的选路实验
uboot sets the default bootdelay
ansible批量安装zabbix-agent
Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
Sturges规则
训练分类器
Map Reduce
利用opencv读取图片,重命名。
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)
SECURITY DAY02 (Zabbix alarm mechanism, Zabbix advanced operation and monitoring case)
windows10安全中心显示“修正未完成”
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-3
【LeetCode】306.累加数(思路+题解)
命令输出给变量
OA项目之我的会议(会议排座&送审)
ETCD集群故障应急恢复-从snapshot恢复
会议OA项目之我的会议
OA项目之待开会议&历史会议&所有会议
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-28

