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 ' /',其中/后的% 就表示%本身,不再是匹配符 

 

posted on 2010-03-17 14:17  巡山小牛  阅读(228)  评论(1)    收藏  举报