myql_DQL查询

一.基础查询

  1.查询表的所有数据

select * from 表名

  2.查询表的一个字段

select 字段1 from 表名

  3.查询表的多个字段

select 字段1,字段2,字段3 from 表名

 

   4.查询函数

select version() #查询版本

  5.取别名(两种方式)

select 字段名1 as 别名 from 表名 #第一种方式
select 字段名1 别名 from 表名 #第二种方式

#如果别名中存在关键字冲突
select 字段名1 "别名" from 表名

  6.去重

select distinct 字段1 from 表名

  拓展:+号的作用

 

   7.拼接多个字段的值(如果存在一个字段为Null,则拼接结果为Null)

select concat('a','b','c') as 别名 from 表名

 

   8.若查询的字段值为null,设置默认值

select ifnull(字段名,默认值) from 表名

二.条件查询

   1.查询工资大于12000的员工(逻辑运算符)

select employ_id, employ_name 
from emploees
where salary>12000 

  2.查询部门编号不等于90的员工数据(逻辑运算符)

select * from emploeys where department_id <> 90

  3.查询工资在12000以上并且部门编号等于90(逻辑运算符)

select * from employees where salary>12000 and department_id = 90

  4.查询员工名第三个字符是n的员工(模糊查询)

select * from employs where employ_name like __n%

  5.查询员工名第二个字符是#的员工(模糊查询)

select * from employs where employee_name like _\#%

  6.查询员工id在100到120的员工数据(between and 是都包含临界值的,并且顺序是不能颠倒,小的值在左边,大的值在右边)

select * form emploeys where emploee_id between 100 and 120

  7.查询员工在10,20,30部门的员工数据(记住 in 后的列表中的所有值类型必须是一致的或者是能转换的,比如说(18,19,'29'),那字符串29就能转换为数字29)

select * form emploeys where department_id in (10,20,30)

  8.查询员工工资不为空的员工数据

select * from employs where salary is not null

 三.排序查询

  1.根据员工的薪资从高到低排序

select * from employs order by salary desc

  2.查询员工编号大于90,并且员工的薪资从低到高排序

select * from employs where employ_id >= 90 order by salary asc

  3.查询员工薪资大于10000的数据,并升序排序

select name,salary*12 年薪 from employs order by 年薪 asc

  4.按员工姓名的字节长度升序排序

select length(name) 字节长度,name from employs order by 字节长度 asc

  5.按员工的年龄升序排序,员工的薪资降序排序

select * from employs order by age asc,salary desc

 

   6.查询员工薪资不在500-1000的数据,并年龄降序排序

select * from employs where salary not between 100 and 500 order by age desc

 

posted @ 2020-11-18 15:23  爱编程DE文兄  阅读(161)  评论(0编辑  收藏  举报