日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

02. SQL表達式的靈活使用

系統(tǒng) 2283 0
原文: 02. SQL表達式的靈活使用

什么是SQL表達式?在SQL語句中,表達式可以是函數(shù),也可以是列和列之間的混合運算。
很多時候,對于表達式的使用,可以比單獨操作表上的列,帶來更多方便。

一. 在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列的數(shù)據(jù)按'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中數(shù)據(jù),先按第一個字符排序,再按第二個字符排序

      
        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
      
    

?聚合函數(shù)中, 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那里的數(shù)字意思不一樣,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表達式的靈活使用


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 桓仁| 广元市| 易门县| 唐山市| 扶绥县| 平安县| 原阳县| 万山特区| 延安市| 高平市| 瓦房店市| 连城县| 临颍县| 内江市| 左权县| 苍梧县| 连平县| 安阳市| 交口县| 台州市| 辉南县| 澄迈县| 永新县| 体育| 会昌县| 武陟县| 洞口县| 长治市| 三明市| 琼中| 龙口市| 宿州市| 茂名市| 呈贡县| 南木林县| 喀什市| 徐汇区| 连江县| 喀喇| 新源县| 孝昌县|