select查询的相关内容
发布时间:2020-12-12 15:42:37 所属栏目:MsSql教程 来源:网络整理
导读:一、select: 查询 、检索 简单的语法: select 字段名列表 from 表名 [where 条件] 作用:显示指定表中满足条件的记录的相关字段信息 示例:显示韩国强用户的姓名、性别、籍贯 select 姓名,性别,籍贯 from yuanGong where 姓名='韩国强' select 姓名,籍贯 f
一、select: 查询 、检索简单的语法: select 字段名列表 from 表名 [where 条件] 作用:显示指定表中满足条件的记录的相关字段信息 示例:显示韩国强用户的姓名、性别、籍贯 select 姓名,性别,籍贯 from yuanGong where 姓名='韩国强' select 姓名,籍贯 from yuanGong --显示所有用户的姓名、性别、籍贯 显示所有男同志的信息: select * from yuanGong where 性别='男' 所有字段可以用*代替二、运算符1、算术运算符:+、 -、 * 、/ 、%(取模、取余数,余数的符号和被除数一样) 示例:select 123%100 --23 select 7%-3 --1 每个人的基本工资提升20%以后是多少: select 基本工资,基本工资*1.2 from yuanGong 2、字符运算符:like、 + 针对字符串 like 像....一样 模糊查询 语法:select 字段名 from 表名 where 字段名 like '表达式' [a-z] 表示从a、b、c、d ...x、y、z的所有字母 [A-Z] 表示从A、B、C、D ...X、Y、Z的所有字母 [^a-z] 表示除了a、b、c、d ...x、y、z之外的所有字符 % 表示通配任意多个任意字符 (*--dos里面) _ 表示通配一个任意字符 (? -- dos里面) 说明:dow里面输入e:&;dir *.jpg /s /a(s为全盘搜索,a为隐藏文件) e:&;dir ???.jpg /s(???表示不超过3个字符的文件) 实例: 查找所有姓李的用户 select * from yuanGong where 姓名 like '李%'(*表示所有字段) 显示姓名中含有李的员工记录 select * from yuanGong where 姓名 like '%李%' 显示邮政编码正确的员工信息 select * from yuanGong where 邮政编码 like '[0-9][0-9][0-9][0-9][0-9][0-9]' 说明:+字符串连接符,用来连接多个字符串 显示邮政编码中含有%号的员工信息 select * from yuanGong where 邮政编码 like '%[%]%' select * from yuanGong where 邮政编码 like '%!%%' escape '!' --转义 3、关系运算符:< 、> 、= 、<= 、>=、!= 显示年龄是22岁的员工记录 select * from yuanGong where 年龄=22 显示技术部之外的员工记录 select * from yuanGong where 部门 <> '技术部' 4、逻辑运算符 not and or 逻辑与: and 真 1 假 0 双目运算(0 and 0 = 0,0 and 1 = 0,1 and 1 = 1) 参与逻辑与运算的条件全部成立时,则结果为真 逻辑或: or 双目运算 参与逻辑或运算的条件只要有一个为真,则结果为真 逻辑非: not 单目运算(not not 1 = 1,not 1 = 0,not 0 = 1) 对现有条件取反 说明:逻辑电路,电子电路 实例: 显示一下姓名是孙萍萍的女同志 select * from yuanGong where 姓名 = '孙萍萍' and 性别 = '女' 显示山东和湖南的女同志记录 select * from yuanGong where 性别 = '女' and 籍贯 = '山东' or 籍贯 ='湖南'and 性别='女' select * from yuanGong where 性别 = '女' and (籍贯 ='山东' or 籍贯='湖南') --括起来表示先运算 说明:and not or同时出现时的顺序: not and or 显示年龄不是20的员工记录 select * from yuanGong where 年龄!=20 select * from yunaGong where not 年龄=20 显示技术部和保安部姓李的员工记录 select * from yuanGong where 姓名 like '李%' and (部门 = '技术部' or 部门 ='保安部')三、数据表的排序1、语法:select 字段名列表 from 表名 [where条件] order by 排序字段名1[asc/desc][,排序字段名2 asc/desc ...] 作用:对指定表中满足条件的记录按照排序字段进行排列,如果排序字段名1的值相同,则按照字段名2进行排列 asc升序 从小到大 desc 降序 从大到小 排序:逻辑排序(不影响记录的物理排放位置) 实例:按照年龄从小到大进行排序 select * from yuanGong order by 年龄 asc --升序 select * from yuanGong order by 年龄 desc --降序 说明:如果不选asc/desc,则默认是asc 按性别进行排列: select * from yuanGong order by 性别 2、比较: 数字:从小到大 字母:从A到Z 汉字:按照新华字典 实例: 按工作日期从小到大进行排列 select * from yuanGong order by 工作日期 asc 按同一个部门的一起显示 select * from yuanGong order by 部门 按照部门进行升序排序,如果部门相同的再按照年龄进行降序排列 select * from yuanGong order by 部门 asc,年龄 desc 3、引入: in(值列表) 实例:如何显示年龄是38、17、20、48的员工记录 select * from yuanGong where 年龄 in (38,17,20,48) 或 select * from yuanGong where 年龄 = 30 or 年龄 = 17 or 年龄 = 20 or 年龄 = 48 4、引入:between 实例:显示年龄在18到40之间的员工记录 select * from yuanGong where 年龄>=18 and 年龄 <=40 或 select * from yuangGong where 年龄 between 18 and 40 5、引入:distinct过滤重复记录 实例:显示本公司的所有部门名称 select distinct 部门 from yuanGong 6、引入:top N 从前面开始显示N条记录 实例:显示前面三条记录select top 3 * from yuanGong 说明:只能从前面显示,不能从中间或者后面显示 MySQL中:select * from yg limit 0,3;(0表示起始位置,3表示总数) 四、 完整的描述对象:服务器名称 . 数据库名称 . 所有者 . 对象名称 select * from server1.dufei.dbo.yuanGong(dbo为数据库所有者) 实例: 显示另一个数据库中的员工表 select * from server1.itet.dbo.yuanGong 简写: select * from itet.dboyuanGong select * from itet..yunaGong五、聚集函数(聚合函数)1、sum() 求和函数 avg() 求平均值函数 max() 求最大值函数 min() 求最小值函数 count() 汇总函数 实例: 求所有员工的年龄总和 select sum(年龄) from yuanGong 求应发工资平均值 select avg(应发工资) from yuanGong 区别:应发工资为空时不参与计算 select sum(应发工资)/12 from yuanGong 求年龄最大的员工记录 select top 1 * from yuanGong order by 年龄 desc 2、count()统计函数 实例:统计男同志记录数 select count(*) from yuanGong where 性别 = '男' select count(婚否) from yuanGong where 部门 = '技术部' count(*) 与 count(字段名)的区别: count(*)连同空值一起统计 count(字段名)不统计空值 3、引入:group by 分组 语法:select 相关字段名 from 表名 [where条件] group by 字段名 实例:求每个部门的平均基本工资 select 部门,avg(基本工资) from yuanGong group by 部门 只显示部门平均基本工资在2500以上的部门,不含2500 注:group by 语句后进行条件过滤,不允许使用where,只能使用having select 部门,avg(基本工资) from yuanGong group by 部门 having avg(基本工资) > 2500 作业:三次考试都在90分以上,找出该学员 A、把90分以下的去掉 select * from cj where 成绩 >=90 B、每个人出现的次数 select 姓名 from cj where 成绩>=90 group by 姓名 having count(*)=3(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |