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

PHP
PHP新手总结的PHP基础知识
php实现gb2312和unicode间编码转换
用php语言实现数据库连接详细代码介绍
详细解析 PHP 向 MySQL 发送数据过程
利用PHP V5开发多任务应用程序
详细讲解PHP中缓存技术的应用
php escapeshellcmd多字节编码漏洞
《PHP设计模式介绍》导言
《PHP设计模式介绍》第一章 编程惯用法
《PHP设计模式介绍》第二章 值对象模式
《PHP设计模式介绍》第三章 工厂模式
《PHP设计模式介绍》第四章 单件模式
《PHP设计模式介绍》第五章 注册模式
《PHP设计模式介绍》第六章 伪对象模式
《PHP设计模式介绍》第七章 策略模式
《PHP设计模式介绍》第八章 迭代器模式
《PHP设计模式介绍》第九章 观测模式
《PHP设计模式介绍》第十章 规范模式
《PHP设计模式介绍》第十一章 代理模式
《PHP设计模式介绍》第十二章 装饰器模式

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


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

10.SQL语句技巧

1、一个SQL语句的问题:行列转换

select * from v_temp

上面的视图结果如下:

user_name role_name

-------------------------

系统管理员 管理员

feng 管理员

feng 一般用户

test 一般用户

想把结果变成这样:

user_name role_name

---------------------------

系统管理员 管理员

feng 管理员,一般用户

test 一般用户

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

create table a_test(name varchar(20),role2 varchar(20))

insert into a_test values('李','管理員')

insert into a_test values('張','管理員')

insert into a_test values('張','一般用戶')

insert into a_test values('常','一般用戶')

create function join_str(@content varchar(100))

returns varchar(2000)

as

begin

declare @str varchar(2000)

set @str=''

select @str=@str+','+rtrim(role2) from a_test where [name]=@content

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

return @str

end

go

--调用:

select [name],dbo.join_str([name]) role2 from a_test group by [name]

--select distinct name,dbo.uf_test(name) from a_test

2、求助!快速比较结构相同的两表

结构相同的两表,一表有记录3万条左右,一表有记录2万条左右,我怎样快速查找两表的不同记录?

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

给你一个测试方法,从northwind中的orders表取数据。

select * into n1 from orders

select * into n2 from orders

select * from n1

select * from n2

--添加主键,然后修改n1中若干字段的若干条

alter table n1 add constraint pk_n1_id primary key (OrderID)

alter table n2 add constraint pk_n2_id primary key (OrderID)

select OrderID from (select * from n1

union

select * from n2) a group by OrderID having count(*) > 1

应该可以,而且将不同的记录的ID显示出来。

下面的适用于双方记录一样的情况,

select * from n1 where orderid in

(

select OrderID from (select * from n1

union

select * from n2) a group by OrderID having count(*) > 1

)

至于双方互不存在的记录是比较好处理的

--删除n1,n2中若干条记录

delete from n1 where orderID in ('10728','10730')

delete from n2 where orderID in ('11000','11001')

--*************************************************************

-- 双方都有该记录却不完全相同

select * from n1 where orderid in

(

select OrderID from (select * from n1

union

select * from n2) a group by OrderID having count(*) > 1

)

union

--n2中存在但在n1中不存的在10728,10730