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

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


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