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

PHP
基于HTTP长连接的"服务器推"技术的php 简易聊天室
PHP 程序员应该使用的10个组件
phpmailer 中文使用说明(简易版)
php 调用远程url的六种方法小结
PHP+XML 制作简单的留言本 图文教程
PHP+MySQL 制作简单的留言本
初学CAKEPHP 基础教程
网页游戏开发入门教程二(游戏模式+系统)
网页游戏开发入门教程三(简单程序应用)
PHP 向右侧拉菜单实现代码,测试使用中
PHP 压缩文件夹的类代码
PHP CKEditor 上传图片实现代码
php 将excel导入mysql
php 向访客和爬虫显示不同的内容
php实现网站插件机制的方法
PHP 远程关机实现代码
超级简单的php+mysql留言本源码
PHP 面向对象实现代码
php 分库分表hash算法
计算一段日期内的周末天数的php代码(星期六,星期日总和)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 87 ::
收藏到网摘: 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

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