原文:02. SQL表达式的灵活使用

什么是SQL表达式?在SQL语句中,表达式可以是函数,也可以是列和列之间的混合运算。
很多时候,对于表达式的使用,可以比单独操作表上的列,带来更多方便。

一. 在HAVING中使用表达式

--drop table t
create table t(c1 int,c2 int)insert into t
select 1,100 union all
select 1,200 union all
select 2,100 union all
select 2,200 union all
select 2,300 union all
select 3,50 union all
select 3,200 union all
select 4,50 union all
select 4,200 union all
select 4,300

返回c1,满足:有3个且都大于等于100 的c2 (学校的考试题中很多见)。

select c1 from t
group by c1
having min(c2)>=100 and count(1)=3

同样,表达式也可以用于group by 子句。

二. 在ORDER BY中使用表达式

--drop table t_orderby
create table t_orderby
(
c1 int null,
c2 varchar(10) null,
c3 varchar(10) null
)insert into t_orderby
select 1,'2','a1' union all
select 1,'1','a2' union all
select 3,'1','ab' union all
select 1,'4','b1'

1. c2列的数据按'4','1','2'的指定顺序排序

(1) 使用union

select * from t_orderby
where c2='4'
union all
select * from t_orderby
where c2='1'
union all
select * from t_orderby
where c2='2'

(2) 使用表达式方法1

select * from t_orderby
order by charindex(c2,'4,1,2') 

(3) 使用表达式方法2,再加个按照c1倒序

select * from t_orderby
order by case
         when c2='4' then 1
         when c2='1' then 2
         when c2='2' then 3
         end,c1 desc

2. 随机排序

(1) 要求c2='4'排第一行,其他的行随机排序

select * from t_orderby
order by case
         when c2='4' then 1
         else 1+rand()
         end

(2) 所有行随机排序

select * from t_orderby
order by newid()

(3) 随机取出第一行

select top 1 * from t_orderby
order by newid()

3. 要求列c3中数据,先按第一个字符排序,再按第二个字符排序

select * from t_orderby
order by left(c3,1),ASCII(substring(c3,2,1))

三. 在COUNT中使用表达式

--drop table t_count
create table t_count
(
c1 varchar(10) null,
c2 varchar(10) null
)insert into t_count values(null,null)
insert into t_count values('a','b')
insert into t_count values('a','b')
insert into t_count values('c','d')

1. 使用常量表达式避免忽略NULL值

select COUNT(c1) from t_count --3
select COUNT(distinct c1) from t_count --2

聚合函数中, SUM/AVG/COUNT中的NULL会被忽略,比如:这里的count(c1)忽略了null

select COUNT(*) from t_count --4
select COUNT(1) from t_count --4
select COUNT(1000) from t_count --4

用count(*)不会忽略NULL,同样用count(1)也不会忽略NULL,这里的1就是一个常量表达式,换成其他常量表达式也可以,比如count(1000)。

另外,count(1)和order by 1,2那里的数字意思不一样,order by后面的序号表示列号。

2. 小心表达式值为NULL被忽略

--正常
select count(*) from (select c1,c2 from t_count group by c1,c2) t --3
select count(*) from (select distinct c1,c2 from t_count) t --3
--有NULL参与了运算,所以表达式值为NULL
select count(distinct c1+c2) from t_count --2

四. 在JOIN中使用表达式

--drop table t1,t2
create table t1
(
url        varchar(1000)
)create table t2
(
code        varchar(1000)
)--insert
insert into t1
select 'http://www.baidu.com/test1' union all
select 'http://www.baidu.com/test2' union all
select 'http://www.baidu.com/test3' union all
select 'www.baidu.com/test1' union all
select 'www.baidu.com/test2' union all
select 'http://www.google.com/test1' union all
select 'http://www.google.com/test2' union all
select 'http://www.sogou.com/test3' union all
select 'http://www.sogou.com/test4'insert into t2
select 'baidu.com' union all
select 'sogou.com'

要求t1,t2表的两个列之间做匹配,t2的列值包含在t1的列值里。

事实上,在join或者where条件中,只要能构造出比较运算表达式(返回boolean值),就可以用作判断条件。

select t2.code,t1.url from t1
inner join t2
on CHARINDEX(t2.code,t1.url) > 0--结果如下
/*
baidu.com    http://www.baidu.com/test1
baidu.com    http://www.baidu.com/test2
baidu.com    http://www.baidu.com/test3
baidu.com    www.baidu.com/test1
baidu.com    www.baidu.com/test2
sogou.com    http://www.sogou.com/test3
sogou.com    http://www.sogou.com/test4
*/

02. SQL表达式的灵活使用相关推荐

  1. sql表达式_SQL表达式

    sql表达式 SQL expression is a combination of one or more values, operators and SQL functions that resul ...

  2. 有两个关系S(A, B, C, D)和T(C, D,E, F), 写出与下列查询等价的SQL表达式:

    题目 如果本题答案不理解,先看这篇文章就明白讲什么意思了.如何画关系代数的连接图?(数据库关系代数中笛卡儿积.θ连接.等值连接.自然连接.外连接) 有两个关系S(A, B, C, D)和T(C, D, ...

  3. 02 SQL语言 实验报告

    02 SQL语言 实验报告 广州大学学生实验报告 开课学院及实验室:计算机科学与工程实验室418B室         2018年05月 09 日 学院 计算机科学与教育软件 年级.专业.班 网络*** ...

  4. SQL学习笔记 | 02 SQL语句结构

    SQL学习笔记 | 02 SQL语句结构 一.表的导入 1.表的命名 2.导入步骤 3.导入需注意 二.标准SQL语法 1.语句结构 2.数据表的其他关键词 3.SQL语句的分类 一.表的导入 1.表 ...

  5. 【JEECG技术文档】数据权限自定义SQL表达式用法说明

    1. 数据权限自定义SQL支持表达式 功能介绍 数据规则通过配置自定义sql来实现数据权限的控制,自定义SQL支持表达式取值 其中自定义sql 条件中字段的名称和数据库表的字段名保持一致. 角色授权 ...

  6. 《天池龙珠 - SQL训练营》02.SQL基础:查询与排序-select、运算符、聚合分组查询等

    本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql 目录 一.SELECT语句基础 ...

  7. 数据库理论 02 SQL——基于《数据库系统概念》第七版

    SQL数据定义 SQL数据定义语言(DDL)可以定义每个关系的信息 关系模式 属性取值范围.属性域 完整性约束(主外码) 关系的安全性和权限信息 其他信息 关系维护的索引集合 关系在磁盘上的物理存储结 ...

  8. 【SQL语句】灵活SQL语句

    1.查询数学成绩排名 select ss.stid, ss.name, ss.score, (select count(*) from stuscore s where s.subject='数学' ...

  9. 【数据库】02 SQL语句

    0 学习目标 数据库表操作. 数据的增删改查操作. 数据查询操作.[重点][难点:关联查询] 1 命令行客户端 1.1 操作数据库 说明:操作数据库之前要选通过命令行工具连接到数据库. 常见数据库操作 ...

最新文章

  1. html 复选框 mysql_Html:实现带复选框的下拉框(一)
  2. Python学习笔记:错误,测试,调试(起)
  3. win10改计算机用户名,win10系统修改本地账号用户名的操作方法
  4. 我的世界变betty指令_Betty Hacker将开放式硬件电子产品嵌入蛋糕中
  5. Anaconda各版本安装包存档
  6. (50)FPGA面试题消除一个glitch实现
  7. c# export server 调用sql_C#调用SQL Server参数过程传参
  8. redis+mybatis+spring
  9. C#百度OCR-身份证图片识别提取信息
  10. oracle client 客户端 安装 oracle客户端安装
  11. B/S网页在线版仓库管理软件的意义何在
  12. 一种文件捆绑型病毒研究
  13. 高数 | 开 闭区间上连续函数的性质及证明
  14. MyBatis_查询缓存01
  15. 2021年CVPR论文Deep Two-View Structure-from-Motion Revisited阅读笔记
  16. 第五期:写一篇高水平的工程类英文论文(SCI/EI)_图和表(Figure and Table)【论文写作】
  17. 个人号微信淘宝客机器人SDK定制开发教程
  18. 开启codelite的c++11
  19. 【网络协议】互联网协议入门(一)
  20. 钽电容的选用和使用标准

热门文章

  1. 订货(bzoj 2424)
  2. 《黑马程序员》认识OC的第一个程序(Objective-c)
  3. IE与Cognos的那些事
  4. 处理js事件时,获取键盘数字注意
  5. jsp connection DB
  6. [原]请留心asp:Image控件中的ImageUrl属性
  7. 可以让你玩儿光剑的程序,vc实现方法和代码
  8. Ubuntu下安装Nginx,PHP5(及PHP-FPM),MySQL
  9. 深度学习综述(LeCun、Bengio和Hinton)
  10. 机器学习和深度学习学习资料