随笔 - 34,  文章 - 1,  评论 - 47,  阅读 - 27911

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   巡山小牛  阅读(216)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示