什么是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. thinkphp过滤html、script

    使用tp3.1版本 1.APP/common 自定义函数 function filter_default(&$value){ $value = htmlspecialchars($value) ...

  2. angular select中ng-options使用

    function selectCtrl($scope) { $scope.selected = ''; $scope.model = [{ id: 10001, mainCategory: '男', ...

  3. C# WinForm开发系列 - RDLC

    http://www.cnblogs.com/peterzb/archive/2009/07/08/1519489.html http://jingyan.baidu.com/article/ab69 ...

  4. MVC的System.Web.Mvc.ViewPage小结

    Inherits="System.Web.Mvc.ViewPage<dynamic>这一句最好是自己手动修改,如果是维护用户数据,用户对象名是User,改成Inherits=&q ...

  5. wikioi 1098 均分纸牌

    题目描述 Description 有 N 堆纸牌,编号分别为 1,2,-, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...

  6. HTML第三天学习笔记

    昨天学的超链接,今天深入学习了下,发现了更多的知识点,而且关于初始新建网页时,由于是初学者,所以还是纯手写代码~ <html> <head> <title>超链接& ...

  7. TCP/IP协议族-----13、运输层简单介绍

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果

    大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei. 在<仙剑奇侠传>.<古剑奇谭>等游戏中,常常须要玩家在一个3D场景中 ...

  9. svn cleanup failed–previous operation has not finished 解决方法

    今天svn遇到一个头疼的问题,最开始更新的时候失败了,因为有文件被锁住了.按照以往的操作,我对父目录进行clean up操作,但是clean up 操作也失败了! svn cleanup failed ...

  10. 黑客破译android开发代码真就那么简单?

    很多程序员辛辛苦苦开发出的android开发代码,很容易就被黑客翻译了. Google似乎也发现了这个问题,从SDK2.3开始我们可以看到在android-sdk-windows\tools\下面多了 ...