当前位置: 首页 > 图文教程 > 网络编程 > PHP > MySQL (C API)VC实例及代码下载 (1)(4)

PHP
PHP中for循环语句的几种“变态”用法
用PHP与XML联手进行网站开发
PHP程序漏洞产生的原因和防范方法
利用PHP编程防范XSS跨站脚本攻击
使用PHP往Windows系统中添加用户
PHP Shell的编写(改进版)
PHP开发中接收复选框信息的方法
PHP程序加速探索之服务器负载测试
PHP实现首页自动选择语言转跳
十天学会php之第一天
十天学会php之第二天
十天学会php之第三天
十天学会php之第四天
十天学会php之第五天
十天学会php之第六天
十天学会php之第七天
十天学会php之第八天
十天学会php之第九天
十天学会php之第十天
Web开发源代码:PHP生成静态页面的类

PHP 中的 MySQL (C API)VC实例及代码下载 (1)(4)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 70 ::
收藏到网摘: n/a

11.1. 行列转换--普通

假设有张学生成绩表(CJ)如下

Name Subject Result

张三 语文 80

张三 数学 90

张三 物理 85

李四 语文 85

李四 数学 92

李四 物理 82

想变成

姓名 语文 数学 物理

张三 80 90 85

李四 85 92 82

declare @sql varchar(4000)

set @sql = 'select Name'

select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'

from (select distinct Subject from CJ) as a

select @sql = @sql+' from test group by name'

exec(@sql)

11.2. 行列转换--合并

有表A,

id pid

1 1

1 2

1 3

2 1

2 2

3 1

如何化成表B:

id pid

1 1,2,3

2 1,2

3 1

创建一个合并的函数

create function fmerg(@id int)

returns varchar(8000)

as

begin

declare @str varchar(8000)

set @str=''

select @str=@str+','+cast(pid as varchar) from 表A where id=@id

set @str=right(@str,len(@str)-1)

return(@str)

End

go

--调用自定义函数得到结果

select distinct id,dbo.fmerg(id) from 表A

11.3. 如何取得一个数据表的所有列名

方法如下:先从SYSTEMOBJECT系统表中取得数据表的SYSTEMID,然后再SYSCOLUMN表中取得该数据表的所有列名。

SQL语句如下:

declare @objid int,@objname char(40)

set @objname = 'tablename'

select @objid = id from sysobjects where id = object_id(@objname)

select 'Column_name' = name from syscolumns where id = @objid order by colid

是不是太简单了? 呵呵 不过经常用啊。