首页 > 其他分享 >05.条件查询

05.条件查询

时间:2022-10-16 21:55:50浏览次数:40  
标签:12 05 People year when 查询 PeopleBirth 条件 select

条件查询

SQL中常用的运算符

=     等于,比较是否相等及赋值
!=    比较不等于
>     比较大于
<     比较小于
>=    比较大于等于
<=    比较小于等于
IS NULL    比较为空
IS NOT NULL  比较不为空
in    比较是否在其中
like   模糊查询
BETWEEN...AND...     比较是否在两者之间
and    逻辑与
or     逻辑或
not   逻辑非
--查询员工为男的信息
select*from People where PeopleSex = '男'
--查询员工工资大于3000的员工信息
select*from People where PeopleSalary > 3000
--查询员工为男且工资大于3000的员工信息
select*from People where PeopleSex = '男' and PeopleSalary>3000
--查询出生日期在1980-1-1之后,月薪大于3000的男员工
select*from People 
where PeopleBirth>='1980-1-1' and PeopleSalary > 3000 
and PeopleSex = '男' 
--查询工资大于5000或者性别为女工资大于3000的员工信息
select*from People 
where PeopleSalary>5000 or (PeopleSex='女' and PeopleSalary>3000)
--查询工资在3000-5000之间的员工信息
select*from People where PeopleSalary >= 3000 and PeopleSalary <= 5000 --第1种
select*from People where PeopleSalary between 3000 and 5000  --第2种
--查询地址在北京或者武汉的员工信息
select*from People where PeopleAddr='武汉' or PeopleAddr='北京'
select*from People where PeopleAddr in ('武汉','北京')
--将员工信息按员工工资进行降序    升序(asc)  降序(desc)
--升序为默认值
select*from People order by PeopleSalary desc
--将员工信息按员工名字的长度进行降序
select*from People order by len(PeopleName) desc
--将员工工资最高的2个人显示出来
select top 2 *from People order by PeopleSalary desc
--将员工最高的3%的员工显示出来 
select top 3 percent *from People order by PeopleSalary desc
--查询员工地址没有填写的员工信息
select*from People where PeopleAddr is null
--查询员工地址填写的员工信息
select*from People where PeopleAddr is not null
--查询80后的员工
select*from People where PeopleBirth>='1980-1-1'
 and PeopleBirth<='1989-12-31'
select*from People where PeopleBirth between '1980-1-1' and '1989-12-31'
select*from People where year(PeopleBirth) between '1980' and '1989'

--查询30岁到40岁之间,工资在4000-7000之间的员工
select*from People where 
(year(getdate()) - year(PeopleBirth) between 30 and 40)
and (PeopleSalary>=4000 and PeopleSalary<=7000)
 
 --查询出出生日期(6.22-7.22)的员工信息
 select*from People where
 (month(PeopleBirth)=6 and day(PeopleBirth)>=22)
 or
 (month(PeopleBirth)=7 and day(PeopleBirth)<=22)

 --查询出工资比5号员工高的员工信息 子查询
 select*from People where 
 PeopleSalary > (select PeopleSalary from People where PeopleId=5)

--查询出生肖为猴的员工信息(2004%12=0)
select*from People where (year(PeopleBirth)%12 = 0)

--查询所有员工信息,添加一列,显示员工的生肖
select *, 
case
	when (year(PeopleBirth)%12 = 4) then '鼠'
	when (year(PeopleBirth)%12 = 5) then '牛'
	when (year(PeopleBirth)%12 = 6) then '虎'
	when (year(PeopleBirth)%12 = 7) then '兔'
	when (year(PeopleBirth)%12 = 8) then '龙'
	when (year(PeopleBirth)%12 = 9) then '蛇'
	when (year(PeopleBirth)%12 = 10) then '马'
	when (year(PeopleBirth)%12 = 11) then '羊'
	when (year(PeopleBirth)%12 = 0) then '猴'
	when (year(PeopleBirth)%12 = 1) then '鸡'
	when (year(PeopleBirth)%12 = 2) then '狗'
	when (year(PeopleBirth)%12 = 3) then '猪'
	else ''
end 生肖
from People 

select *, 
case year(PeopleBirth)%12 
	when  4 then '鼠'
	when  5 then '牛'
	when  6 then '虎'
	when  7 then '兔'
	when  8 then '龙'
	when  9 then '蛇'
	when  10 then '马'
	when  11 then '羊'
	when  0 then '猴'
	when  1 then '鸡'
	when  2 then '狗'
	when  3 then '猪'
	else ''
end 生肖
from People 

标签:12,05,People,year,when,查询,PeopleBirth,条件,select
From: https://www.cnblogs.com/rain-blogs/p/16797308.html

相关文章

  • pymysql 连接、关闭、查询数据库代码
    1defget_conn():2"""3:return:连接,游标4"""5#创建连接6conn=pymysql.connect(host="127.0.0.1",7use......
  • MyBatis 模糊查询时对特殊字符"%"和"_"的处理
    MyBatis 模糊查询时对特殊字符"%“和”_"的处理问题:输入"%“或”_",查询结果为全部数据,且无法查询到带有"%"或者下划线的数据。解决:对特殊字符转义例如查询字段为na......
  • 6205.反转之后不同整数的数目
    LeetCode-315-2022-10-1610:306205.反转之后不同整数的数目给你一个由正整数组成的数组nums。你必须取出数组中的每个整数,反转其中每个数位,并将反转后得到的数字添加......
  • linux 下HBA相关查询
     1、查看当前卡的品牌,常用的卡有两种,Emulex和Qlogic。---------------------------------------------lspci|grep-ifibre2、查看HBA卡的驱动版本emulex:modinfolp......
  • dql 分组查询和条件查询
    分组查询 公式:select字段列表from表名【where分组前的条件】groupby分组字段名 【having 分组之后的查询条件】注意:分组之后查询字段只能写分组字段名......
  • 2022-2023-1 20221405 《计算机基础与程序设计》 第七周学习总结
    作业信息这个作业属于哪个课程2022-2023-1-计算机基础与程序设计这个作业要求在哪里2022-2023-1计算机基础与程序设计第七周作业这个作业的目标数组与链表......
  • navicat连接mysql数据库提示失败,提示2059错误
    Navicat连接数据库失败,提示2059错误,如下图参考资料发现使用的数据库版本是mysql8版本,不是mysql5.7版本MYSQL新版本(8以上版本)的用户登录账户加密方式是【caching_sha2_passwo......
  • 3.MongoDB系列之查询
    1.find简介//查询所有文档db.users.find({})//查询指定条件文档db.users.find({'name':'shenjian'})//查询指定字段,1查询键0剔除键db.users.find({'name':'sh......
  • SpringBoot整合ES查询
    springboot整个es有很多钟方法,比如TransportClient、RestClient、RestHighLevelClient、SpringData-Es、Elasticsearch-SQL等。ElasticSearch官方提供了3个Client,具体......
  • ES大数据量的分页查询
    FROM/SIZE分页查询默认情况下,不加from,size的话,ES会返回前10条记录。加上from,size就会查询指定的条数。其中from代表起始行号,size代表查询行数。如果用JAVA等Client端传参......