以面看點系列之數據庫篇

1.    數據庫行列互換

/*            citytemp表
----------------------------------------------------------------  
  城市 | 日期 | 溫度 |  
  ----------------------------------------------------------------  
  長沙 | 2004-12-21 | 23 |  
  ----------------------------------------------------------------  
  湘潭 | 2004-12-21 | 28 |  
  ----------------------------------------------------------------  
  株洲 | 2004-12-21 | 26 |  
  ----------------------------------------------------------------  
  長沙 | 2004-12-22 | 27 |  
  ----------------------------------------------------------------  
  湘潭 | 2004-12-22 | 25 |  
  ----------------------------------------------------------------  
  株洲 | 2004-12-22 | 22 |  
  ----------------------------------------------------------------  
  現在顯示爲:   行列互換
  -------------------------------------------------------------------------  
  日期             | 長沙 | 湘潭 | 株洲 |  
  -------------------------------------------------------------------------  
  2004-12-21 | 23    | 28    | 26 |  
  -------------------------------------------------------------------------  
  2004-12-22 | 27    | 25    | 22 |  
  -------------------------------------------------------------------------  

*/

========================================================

2.  like 關鍵字中的通配符可以有哪些?分別有什麼含義?

 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  答案 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

1. 數據庫行列互換

         -- 講解如下:
--  少量城市名個數(寫死)
select CDate,
 max(case city when '長沙' then ctemp else 0 end) as '長沙',
 max(case city when '湘潭' then ctemp else 0 end) as '湘潭',
 max(case city when '株洲' then ctemp else 0 end) as '株洲'
from citytemp
group by CDate
order by cdate

 

-- 靈活的城市名個數(寫活)

--先來看這樣的寫法

select city
from citytemp
group by city

-- 可以得到任意多個城市名

--那麼如下的select 命令寫法就是想拼出下面的字符串來:

select   @s=@s+','+city+'=max(case   city   when   '''+city+'''   then   ctemp   else   0   end)'  
  from   citytemp    
  group   by   city   

/*       上述命令 @s字符串就拼出下面的字符串來

select   cdate   ,長沙=max(case   city   when   '長沙'   then   ctemp   else   0   end),
               湘潭=max(case   city   when   '湘潭'   then   ctemp   else   0   end),
        株洲=max(case   city   when   '株洲'   then   ctemp   else   0   end)
*/

-- 但上述字符串命令是無法執行的,然後再拼如下的字符串:
-- set   @s=@s+'   from   citytemp   group   by   cdate'  

-- 完整的命令如下:
declare   @s   varchar(500)  
   
  set   @s='select   cdate   '  
  select   @s=@s+','+city+'=max(case   city   when   '''+city+'''   then   ctemp   else   0   end)'  
  from   citytemp    
  group   by   city    
  set   @s=@s+'   from   citytemp   group   by   cdate'  
  exec(@s)  

--結果正確,並且可以應對任意多個城市名的情況

 

2.  like 關鍵字中的通配符可以有哪些?  

操作符 LIKE 輸入模糊邏輯領域,使用在當SELECT查詢中的WHERE從句的標準只有部分確定的時候。它使用通配符的變化來指定值得缺失部分。必須跟在LIKE關鍵字後面

通配符

描述

使用DB版本

%

匹配任何0個或以上字符

Oracle, IBM DB2 UDB, MS SQL Server 2000

_

匹配任何一個字符

Oracle, IBM DB2 UDB, MS SQL Server 2000

[ ]

匹配任何單個指定的的字符範圍內的字符

Microsoft SQL 2000 only

[ ^ ]

匹配任何單個指定不再指定範圍內的字符

Microsoft SQL 2000 only

在ORACLE中,可以用ESCAPE指定轉義符,如果希望查詢列中包含%的行,就可以使用 col like '%/%%' escape ' /',其中/後的% 就表示%本身,不再是匹配符 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章