什么是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,'','a1' union all
select 1,'','a2' union all
select 3,'','ab' union all
select 1,'','b1'

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

(1) 使用union

select * from t_orderby
where c2=''
union all
select * from t_orderby
where c2=''
union all
select * from t_orderby
where c2=''

(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='' then 1
when c2='' then 2
when c2='' then 3
end,c1 desc

2. 随机排序

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

select * from t_orderby
order by case
when c2='' 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 --
select COUNT(distinct c1) from t_count --

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

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

用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 --
select count(*) from (select distinct c1,c2 from t_count) t --
--有NULL参与了运算,所以表达式值为NULL
select count(distinct c1+c2) from t_count --

四. 在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的列值里。

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
*/

CHARINDEX是做硬匹配,如果是要模糊匹配,可以使用like或者patindex,通配符可以存在表中字段里,也可以在SQL语句中添加,注意like不加通配符的效果和等于(=)一样

--drop table t1,t2
create table t1
(
url varchar(1000)
) create table t2
(
code varchar(1000)
) --insert
insert into t1
select 'baidu%' union all
select 'baidu' union all --CANNOT be found without wildcard like %
select 'baidu.com' union all --identical string can also be found
select 'XXXbaidu.comXXX' union all --CANNOT be found without wildcard like %
select 'sogou%' insert into t2
select 'baidu.com' union all
select 'sogou.com' --like
select t2.code,t1.url
from t1
inner join t2
on t2.code like t1.url
--on t2.code like t1.url+'%'
/*
baidu.com baidu%
baidu.com baidu.com
sogou.com sogou%
*/ --patindex
select t2.code,t1.url
from t1
inner join t2
on patindex(t1.url,t2.code)>0
/*
baidu.com baidu%
baidu.com baidu.com
sogou.com sogou%
*/

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

02. SQL表达式的灵活使用的更多相关文章

  1. Azure SQL 数据库的灵活缩放预览版简介

    Eron Kelly SQL Server 产品管理部门产品市场营销总经理 几天前,我们宣布了发布 Azure SQL 数据库的灵活缩放公共预览版.新增的灵活缩放功能通过简化开发和管理,简化了扩展和缩 ...

  2. SQLAlchemy 学习笔记(一):Engine 与 SQL 表达式语言

    个人笔记,如有错误烦请指正. SQLAlchemy 是一个用 Python 实现的 ORM (Object Relational Mapping)框架,它由多个组件构成,这些组件可以单独使用,也能独立 ...

  3. mybatis 动态sql表达式相关应用

    一.mybatis 表达式简介 对于mybatis3 ,提供了一种动态sql的方式.通过动态sql我们可以直接在mybatis 的xm映射文件中直接通过条件判断的方式进行查询添加的拼接.mybatis ...

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

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

  5. 使用MyBatis的动态SQL表达式时遇到的“坑”(integer)

    现有一项目,ORM框架使用的MyBatis,在进行列表查询时,选择一状态(值为0)通过动态SQL拼接其中条件但无法返回正常的查询结果,随后进行排查. POJO private Integer stat ...

  6. JDK8新特性02 Lambda表达式02_Lambda语法规则

    //函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...

  7. 02 SQL 执行

    sql 被保存在 share pool 后, 开始解析, 解析包括语句的语法, 检验及对象, 以确认该用于是否有该对象的权限, 如果这些都通过了, 接下来就要看这个语句之前是否被执行过, 如果是, o ...

  8. C#3.0新增功能09 LINQ 标准查询运算符 02 查询表达式语法

    连载目录    [已更新最新开发文章,点击查看详细] 某些使用更频繁的标准查询运算符具有专用的 C# 语言关键字语法,使用这些语法可以在查询表达式中调用这些运算符. 查询表达式是比基于方法的等效项更具 ...

  9. javaScript基础-02 javascript表达式和运算符

    一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...

随机推荐

  1. How Tomcat Works(九)

    本文接下来描述servlet容器是怎样管理其相关组件的生命周期的,首先本人描述一下事件监听模式,也可以称为观察者模式,该模式分为以下角色 即抽象主题角色 具体主题角色 抽象观察者角色及具体观察者角色, ...

  2. ELF学习--可执行文件

    承接上一篇.我们来看看可执行文件和重定位文件的不同点. readelf -h test 相比较重定位文件,可执行文件的ELF header中入口地址是0x8048320.而且除了section hea ...

  3. CentOS6 下MySQL option file

    my.cnf内容如下 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/e ...

  4. 一个python

    #!/usr/bin/env python #coding=utf-8 import os # 遍历文件 r=input("type a directory name:") for ...

  5. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  6. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

  7. WAF指纹探测及识别技术<freebuf>

    Web应用防护系统(也称:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).利用国际上公认的一种说法:Web应用防火墙是通过执行一系列针对HTTP/HT ...

  8. ZTE AD3812 3G模块在linux 2.6.34 内核的开发板上的支持方法

    先说段废话,话说在linux 2.6.34 下,好多比较新的3G网卡及3G模块都没有很好的支持.如果想支持的这些3G网卡/3G模块呢,基本上有两种方式: 1.使用该3G模块的 linux 下的驱动,交 ...

  9. Python 数据类型

    数据类型计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义 ...

  10. ssh 命令行通过私钥登录其它设备

    ssh -i root(私钥文件) root@IP (被访问的服务器IP) 这里备份一下了