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

05.条件查询

时间:2022-10-17 12:34:02浏览次数:69  
标签: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/Starry-blog/p/16797308.html

相关文章

  • 04.基本查询
    基本查询--查询所有列所有行--*代表查询所有列,未加限制条件说明查找所有行select*fromDepartmentselect*from[Rank]select*fromPeople--查询员工表中(姓名,性别......
  • QFramework v1.0 使用指南 工具篇:05. ResKit 资源管理&开发解决方案
    ResKit简介ResKit,是资源管理&快速开发解决方案特性如下:可以使用一个API从dataPath、Resources、StreammingAssetPath、PersistentDataPath、网络等地方加载资......
  • 多表查询外连接和多表查询_子查询概述
    多表查询外连接:多表查询外连接有两个:一个是左外连接查询,另一个是右外连接查询其实这两个你只需要掌握其中一个就行左外连接查询的例子:SELECTemp.*,dept.`name`FROM......
  • 多表查询概述和多表查询内连接
     多表查询概述:多表查询的意思是一次查多个表,SQL语句上不是有这样的一句语句吗SELECT 列表字段名FROM 列表表名,这就意味着from后面也可以跟多个列表表名比如:SELECT*......
  • 物料查询F4帮助增强(添加筛选条件)----方式一
    货铺QQ群号:834508274进群统一修改群名片,例如BJ_ABAP_森林木。群内禁止发广告及其他一切无关链接,小程序等,进群看公告,谢谢配合不修改昵称会被不定期踢除,谢谢配合需求:1、通过F4......
  • 物料查询F4帮助增强(添加筛选条件)----方式二
    货铺QQ群号:834508274进群统一修改群名片,例如BJ_ABAP_森林木。群内禁止发广告及其他一切无关链接,小程序等,进群看公告,谢谢配合不修改昵称会被不定期踢除,谢谢配合需求:1、通过F4......
  • 105. 从前序与中序遍历序列构造二叉树
    题目描述给定两个整数数组 preorder和inorder ,其中 preorder是二叉树的先序遍历,inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。输入:preorder=[......
  • |软件技术基础|<>||-----------|-----------||介绍我自己|<详细的介绍我自己的兴趣爱
    软件技术基础https://edu.cnblogs.com/campus/zjlg/22rjjc这个作业的目标<了解博客园,详细的介绍我自己>姓名-学号<王芳>-<2020330301057>一、自我介......
  • 基于redis的查询业务缓存实现
    添加缓存业务流程及代码实现业务流程说明:1、先从redis中进行查询,redis中如果有对应的数据则直接返回;如果没有再进入数据库查询2、从数据库查询到的数据判断是否为空......
  • 09.多表查询
    多表查询一、笛卡尔乘积--笛卡尔乘积--查询结果将People所有记录和Department所有记录依次排列组合形成新的结果select*fromPeople,Department;二、简单的多表查询......