12
返回列表 发新帖
楼主: oracle96

这种情况如何用SQL语句实现

[复制链接]
论坛徽章:
3
授权会员
日期:2005-10-30 17:05:33管理团队2006纪念徽章
日期:2006-04-16 22:44:45会员2006贡献徽章
日期:2006-04-17 13:46:34
11#
发表于 2002-10-4 03:59 | 只看该作者

Re: 这种情况如何用SQL语句实现

You can use outer join to get what you wanted.

Try this:

create table books (book# interger, author char(30));
insert into books values(123, 'Peter');
insert into books values(123, 'David');
insert into books values(123, 'John');


with temp as (select c1.book# as book, c1.author as author1, c2.author as author2, c3.author as author3
from books as c1 left outer join books as c2
on c1.book# = c2.book# left outer join books as c3 on c2.book# = c3.book#
and c1.author = 'Peter' and c2.author = 'David' and c3.author = 'John')
select * from temp where author1 is not null and author2 is not null and author3 is not null;


Jimruski


最初由 oracle96 发布
[B]I have a table about the book and author relationship.

Book    Author
----    ------
123     Peter
123     David
123     John

How can I use a single sql query to get the result:

Book    Author
----    ------
123     Peter,David,John [/B]

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
12#
发表于 2002-10-5 09:32 | 只看该作者

Re: Re: 这种情况如何用SQL语句实现

最初由 jimruski 发布
[B]You can use outer join to get what you wanted.

Try this:

create table books (book# interger, author char(30));
insert into books values(123, 'Peter');
insert into books values(123, 'David');
insert into books values(123, 'John');


with temp as (select c1.book# as book, c1.author as author1, c2.author as author2, c3.author as author3
from books as c1 left outer join books as c2
on c1.book# = c2.book# left outer join books as c3 on c2.book# = c3.book#
and c1.author = 'Peter' and c2.author = 'David' and c3.author = 'John')
select * from temp where author1 is not null and author2 is not null and author3 is not null;


Jimruski


[/B]


如果author是2个呢,是4个呢?你这个太不通用了。

使用道具 举报

回复
论坛徽章:
0
13#
发表于 2002-10-8 11:33 | 只看该作者
为什么非要用一个sql实现,有何好处?费解!

使用道具 举报

回复
论坛徽章:
13
生肖徽章2007版:虎
日期:2009-03-14 11:55:56ITPUB社区12周年站庆徽章
日期:2013-10-08 15:00:34灰彻蛋
日期:2013-03-16 23:13:202013年新春福章
日期:2013-02-25 14:51:24紫蛋头
日期:2013-01-23 13:48:22双黄蛋
日期:2011-07-30 15:08:322010年世界杯参赛球队:德国
日期:2010-05-11 21:28:202010年世界杯参赛球队:意大利
日期:2010-01-26 11:28:16生肖徽章2007版:兔
日期:2009-05-13 09:35:07生肖徽章2007版:鸡
日期:2009-04-08 19:14:38
14#
发表于 2002-10-8 13:02 | 只看该作者
很多功能不应该在数据库上纠缠,前台实现是很容易的。

使用道具 举报

回复
论坛徽章:
0
15#
发表于 2002-10-8 13:44 | 只看该作者
非常赞成提高sql写作水平,但不要矫枉过正!
论坛上有太多的单句sql偏执狂,精神可嘉,但效果未必最佳!

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表