由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 问一个SQL Query
相关主题
请教一个queryA SQL query never stop running is bothering me: help needed (转载)
请教一个SQL Queryoracle和XML
Merge table with one single query?帮帮忙
please help with this left join questionHow to query a tree
一个SQL query的问题[转载] access问题请教
[转载] question about SQL in Access怎样实现这个query
[转载] Can anyone interpret this simple SQL?Java SQL --> resultset!!
parameterized queries with no inputsSQL question...
相关话题的讨论汇总
话题: query话题: reader话题: field话题: bookid话题: book
进入Database版参与讨论
1 (共1页)
k*****t
发帖数: 161
1
问一个Squery
表 book
Field 1: bookID
Field 2: bookName
bookID 是primary key
bookName is unique
表 reader
Field 1: bookID
Field 2: readerName
表reader的主键是 (bookID, readerName)
现在需要找出所有的两本书,它们有相同的读者群。
例如
表book中的记录是:
1,a
2, b
3, c
表reader中的记录是:
1, w
1, x
2, w
2, x
3, y
Query出来的结果是
a
b
这个 Query怎么写?
Thanks.
y*l
发帖数: 334
2
哪位高手回答一下。
我用了两个query:
select readerBookID,count(readerBookID)From Reader
group by readerBookID
having count(readerBookID)>=2;
得出答案是1,2
然后再回表Book找到a,b
但这种方法在很多records的情况下肯定是不行的。谁教一下怎么合并两个query?谢了先


【在 k*****t 的大作中提到】
: 问一个Squery
: 表 book
: Field 1: bookID
: Field 2: bookName
: bookID 是primary key
: bookName is unique
: 表 reader
: Field 1: bookID
: Field 2: readerName
: 表reader的主键是 (bookID, readerName)

k*****t
发帖数: 161
3
我自己倒有个做法,只是想看别人有什么别的方法。
我的是这样的:
select b1.name, b2.name
from book b1, book b2
where
b1.bookID != b2.bookID and
not exists (select 1 from reader r1, reader r2
where
r1.bookID=b1.bookID and
r2.bookID=b2.bookID and
r1.readerName != r2.readerName)
BTW, 来这里的人好少。



【在 y*l 的大作中提到】
: 哪位高手回答一下。
: 我用了两个query:
: select readerBookID,count(readerBookID)From Reader
: group by readerBookID
: having count(readerBookID)>=2;
: 得出答案是1,2
: 然后再回表Book找到a,b
: 但这种方法在很多records的情况下肯定是不行的。谁教一下怎么合并两个query?谢了先
: 。

s*****c
发帖数: 24
4
This is what I thought, not tested yet:
select book1.bookName, book2.bookName
from book book1, book book2
where book1.bookID <> book2.bookID and
not exists
(select *
from reader r1
where (r1.bookID = book1.bookID and
not exists(select * from reader r2
where r2.bookID = book2.bookID and
r2.readerName != r1.readerName))

【在 k*****t 的大作中提到】
: 问一个Squery
: 表 book
: Field 1: bookID
: Field 2: bookName
: bookID 是primary key
: bookName is unique
: 表 reader
: Field 1: bookID
: Field 2: readerName
: 表reader的主键是 (bookID, readerName)

1 (共1页)
进入Database版参与讨论
相关主题
SQL question...一个SQL query的问题
高手请进,问个SQL问题[转载] question about SQL in Access
THANKS Re: 高手请进,问个SQL问题[转载] Can anyone interpret this simple SQL?
pls help me in this Sql queryparameterized queries with no inputs
请教一个queryA SQL query never stop running is bothering me: help needed (转载)
请教一个SQL Queryoracle和XML
Merge table with one single query?帮帮忙
please help with this left join questionHow to query a tree
相关话题的讨论汇总
话题: query话题: reader话题: field话题: bookid话题: book