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

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)(2)


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