我们如何在一个表中检索相同的数据?

时间:2014-09-23 13:31:09

标签: sql oracle

我需要一个非常专家的帮助,我似乎无法找到答案,问题是,找到与至少另外2本书相同的书籍和出版商的书名和ISBN。< / p>

按标题订购。

所以基本上,一切都来自书桌。

CREATE TABLE BOOK 
(
   ISBN VARCHAR2(20),
   Title VARCHAR2(250) NOT NULL,
   YearPublished NUMBER(4) NOT NULL,
   PublisherName VARCHAR2(40) NOT NULL,

   CONSTRAINT pk_isbnP PRIMARY KEY(ISBN)
);

这是我的草稿:

select _____
from book b2
where ______ (select____ from book b2
             where _____ = ______ and ______ =______)

4 个答案:

答案 0 :(得分:1)

第1步:找到至少有2本书的标题和出版商组合:

SELECT title, PublisherName 
FROM BOOK
GROUP BY title, PublisherName 
HAVING COUNT(*) > 1

第2步:找到所有其他拥有此出版物和出版商的图书:

SELECT *
FROM Books b1
WHERE EXISTS(
    SELECT title, PublisherName 
    FROM BOOK b2
    WHERE b1.title = b2.title AND b1.PublisherName = b2.PublisherName 
    GROUP BY title, PublisherName 
    HAVING COUNT(*) > 1
)

答案 1 :(得分:0)

select b1.title, b1.isbn 
from book b1 
inner join 
  (select title, publishername 
   from book
   group by title, publishername having count(*) > 2) b2 
on b1.title = b2.title and b1.publishername = b2.publishername
order by b1.title

内部查询提取有三个或更多重复项的书籍的标题/发布者。外部查询使用这些结果来获取相关的ISBN。

答案 2 :(得分:0)

您可以在此处使用分组声明。

SELECT title
      ,isbn
  FROM BOOKS
  group by title, isbn
  having COUNT(1) >= 2 order by title;

答案 3 :(得分:0)

应该这样做:

select isbn, title
from books
where (title,publishername) in(select title,publishername 
                                 from books
                                 group by title,publishername
                                 having count(*) >=3)
order by title;