谁能帮我找到2005年或之前出版的书籍?

时间:2013-12-19 05:52:23

标签: mysql sql datetime select group-by

显示提供主题ID为MySQL的书籍和主题ID为DB的图书的发布者的发布者ID和发布者名称。这些书必须有2005年或更晚的出版年份。这些不一定是同一本书。每个符合测试的发布商只显示一行。

我的解决方案:

  select DISTINCT publ_id, publ_name
from a_bkinfo.publishers 
join a_bkinfo.books using (publ_id)
join a_bkinfo.book_topics using (book_id)
join a_bkorders.order_details using (book_id)
join a_bkorders.order_headers USING (order_id)
where book_id in    (
        Select book_id   
        From a_bkinfo.book_topics     
        Where topic_id = 'SQL'
        )
 And book_id in  (    
        Select book_id   
        From a_bkinfo.book_topics     
        Where topic_id = 'DB'
        )
 And book_id in
       Select book_id    
        From a_bkinfo.book_topics
        Where year(order_date) > 2005 

        ); 

以下是表格:

   -- create publishers
create table a_bkinfo.publishers (
   publ_id           integer          not null
 , publ_name         varchar(25)      not null  
 , constraint bk_publishers_pk        primary key(publ_id) 
 , constraint publ_id_range check (publ_id >1000)
)engine = INNODB;

  -- create books
create table a_bkinfo.books (
    book_id           integer          not null
  , title             varchar(75)      not null 
  , publ_id           integer          null
  , year_publd        integer          not null
  , isbn              varchar(17)      null
  , page_count        integer          null  
  , list_price        numeric(6,2)     null  
  , constraint bk_books_pk             primary key (book_id)
  , constraint bk_books_publ_fk        foreign key(publ_id) 
               references a_bkinfo.publishers  (publ_id)
  , constraint book_id_range           check (book_id > 1000)
  , constraint bk_page_count_ck        check (page_count >= 0)
  , constraint bk_price_ck             check (list_price >= 0)             
  , constraint bk_books_year_ck        check (year_publd >= 1850)
)engine = INNODB;

  -- create book_topics
create  table a_bkinfo.book_topics (
    book_id           integer          not null
  , topic_id          varchar(5)       not null  
  , constraint bk_book_topics_pk        primary key (book_id, topic_id)
  , constraint bk_books_topics_fk      foreign key(topic_id) 
               references a_bkinfo.topics(topic_id)
  , constraint bk_books_id_fk          foreign key(book_id) 
               references a_bkinfo.books(book_id)
)engine = INNODB;

  -- create order_headers 
create  table   a_bkorders.order_headers (
    order_id          integer          not null 
  , order_date        date             not null
  , cust_id           integer          not null 
  , constraint bk_orders_pk            primary key (order_id)
  , constraint bk_orders_cust_fk       foreign key(cust_id) 
               references a_bkorders.customers(cust_id) 
  , constraint bk_order_id_range       check (order_id > 100)
 ,  constraint bk_order_date_ck        check (order_date >=  '2000-01-01')
)engine = INNODB;

  -- create order_details  
create  table   a_bkorders.order_details (
    order_id          integer          not null 
  , order_line        integer          not null 
  , book_id           integer          not null 
  , quantity          integer          not null 
  , order_price       numeric(6,2)     not null   
  , constraint bk_orderline_pk         primary key (order_id, order_line)
  , constraint bk_orderline_order_fk   foreign key (order_id) 
               references a_bkorders.order_headers(order_id) on delete cascade
  , constraint bk_orderline_book_fk    foreign key (book_id )  
               references a_bkinfo.books(book_id)
  , constraint bk_quantity_ck          check (quantity > 0) 
  , constraint bk_ordprice_ck          check (order_price >= 0) 
)engine = INNODB;

以下是一些插页:

       -- publishers
    Insert into a_bkinfo.publishers values (9000, 'Microsoft Press') ;
    Insert into a_bkinfo.publishers values (9456, 'New Directions') ;
    Insert into a_bkinfo.publishers values (9102, 'Alfred A. Knopf') ;
    Insert into a_bkinfo.publishers values (9325, 'Addison Wesley') ;
    Insert into a_bkinfo.publishers values (9745, 'Morgan Kaufmann') ;
    Insert into a_bkinfo.publishers values (9521, 'Benjamin/Cummings') ;
    Insert into a_bkinfo.publishers values (9822, 'O''Reilly') ; 
    Insert into a_bkinfo.publishers values (9030, 'McGraw Hill') ;
    Insert into a_bkinfo.publishers values (9444, 'APress') ;
    Insert into a_bkinfo.publishers values (9528, 'Manning');
    Insert into a_bkinfo.publishers values (9020, 'Princeton Univer Press') ;
    Insert into a_bkinfo.publishers values (9021, 'Yale University Press') ;
    Insert into a_bkinfo.publishers values (9022, 'Havard University Press') ;
    Insert into a_bkinfo.publishers values (9507, 'J.Q. Vanderbildt');
    Insert into a_bkinfo.publishers values (9664, 'WROX') ;
    Insert into a_bkinfo.publishers values (9825, 'MySQL Press') ;
    Insert into a_bkinfo.publishers values (9623, 'Prentice Hall') ;
    Insert into a_bkinfo.publishers values (9725, 'Springer') ;
    Insert into a_bkinfo.publishers values (9561, 'Houghton Mifflin'); 
    Insert into a_bkinfo.publishers values (9902, 'W.W. Norton ') ;
    Insert into a_bkinfo.publishers values (9023, 'Holt Paperbacks') ;
    Insert into a_bkinfo.publishers values (9024, 'Univ of California Press') ;
    Insert into a_bkinfo.publishers values (9776, 'Simon and Schuster') ;


    -- topics
    Insert into a_bkinfo.topics values ('ADO',   'ADO');
    Insert into a_bkinfo.topics values ('CMP',   'Computer Science');
    Insert into a_bkinfo.topics values ('DB',    'Database Systems');
    Insert into a_bkinfo.topics values ('FCT',   'Fiction');
    Insert into a_bkinfo.topics values ('HIST',  'History');
    Insert into a_bkinfo.topics values ('MySQL', 'MySQL Database');
    Insert into a_bkinfo.topics values ('NET',   '.NET Technologies');
    Insert into a_bkinfo.topics values ('NOSQL', 'Alternate Data Storage');
    Insert into a_bkinfo.topics values ('ORA',   'Oracle Database');
    Insert into a_bkinfo.topics values ('POE',   'Poetry');
    Insert into a_bkinfo.topics values ('PGM',   'General Programming');
    Insert into a_bkinfo.topics values ('SCI',   'Science');
    Insert into a_bkinfo.topics values ('SQL',   'SQL');
    Insert into a_bkinfo.topics values ('SSRV',  'SQL Server Database');
    Insert into a_bkinfo.topics values ('VB',    'Visual Basic');
    Insert into a_bkinfo.topics values ('XML',   'XML Techniques');
    Insert into a_bkinfo.topics values ('ART',   'Arts, Photography');


    -- books  
    insert into a_bkinfo.books values (1101, 'Programming SQL Server with VB.NET',              9000, 2002, '0735615357',    300, 59.99);
    insert into a_bkinfo.books values (1102, 'Practical Standards for VB.NET',                  9000, 2003, '0735613568',    250, 49.99);
    insert into a_bkinfo.books values (1103, 'Selected Poems',                                  9456, 1949,  null,           125, 12.00);
    insert into a_bkinfo.books values (1104, 'Sibley Guide to Bird Life and Behavior',          9102, 2001, '0679451234',    604, 45.00);
    insert into a_bkinfo.books values (1105, 'SQL:1999 Relational Language Concepts',           9745, 2002, '1558604561',    450, 59.95);
    insert into a_bkinfo.books values (1106, 'SQL for Smarties',                                9745, 1995, '1558603239',    250, 29.00);
    insert into a_bkinfo.books values (1107, 'SQL Puzzles and Answers',                         9745, 1997, '1558604537',    325, 25.00);
    insert into a_bkinfo.books values (1108, 'Database Systems',                                9325, 1996,  null,           680, 39.95);
    insert into a_bkinfo.books values (1109, 'Intro to DB Systems-7th Ed',                      9325, 2000, '0201385902',    650, 80.00);
    insert into a_bkinfo.books values (1110, 'Adv SQL:1999 Object_Relational Features',         9745, 2002, '1558606077',    520, 59.95);


    insert into a_bkinfo.books values (1128, 'Temporal Data and the Relational Model',          9325, 2003, 'na',            275, 49.95);
    insert into a_bkinfo.books values (1133, 'Leaves of Grass',                                 9623, 1902,  null,           125, 19.95);
    insert into a_bkinfo.books values (1142, 'Relational Database Theory',                      9521, 1993,  null,           879, 95.00);
    insert into a_bkinfo.books values (1161, 'SQL Programming Style',                           9745, 2005, '0120887975',    780, 35.00);
    insert into a_bkinfo.books values (1162, 'Trees and Hierarchies',                           9745, 2004, '1558609202',    350, 35.00);
    insert into a_bkinfo.books values (1180, 'MySQL Database Design and Tuning',                9825, 2005, '9780672234650', 400, 49.99);
    insert into a_bkinfo.books values (1175, 'MySQL in a Nutshell',                             9822, 2008, '9780596514331', 538, 34.99);
    insert into a_bkinfo.books values (1182, 'MySQL Cookbook',                                  9822, 2007, '9780596527082', 918, 49.99);
    insert into a_bkinfo.books values (1185, 'MySQL Stored Procedures',                         9822, 2007, '9780596100896', 595, 49.99);
    insert into a_bkinfo.books values (1184, 'MySQL Developer''s Library',                      9325, 2009, '9780672329388', 650, 49.99);
    insert into a_bkinfo.books values (1301, 'ADO and Oracle Workbook',                         9000, 2002, '0265615357',      0, 59.99);
    insert into a_bkinfo.books values (1302, 'ADO: the ebook',                                  9000, 2002, '0852515358',   null, 49.99);
    insert into a_bkinfo.books values (1303, 'Rainbows and Rainbows',                           9521, 2002, '0657895157',   null, 59.99);
    insert into a_bkinfo.books values (1304, 'Stories of Discoveries',                          9325, 2002, '0777788887',    300, 59.99);
    insert into a_bkinfo.books values (1305, 'Journeys Through Flatland',                       9325, 1958, '0387515357',    100,  9.99);
    insert into a_bkinfo.books values (1306, 'Myths of SQL',    

                                9664, 2000, '0454615027',   2895,259.99);
Insert into a_bkorders.order_headers    values(30822,  '2012-03-12', 211483);
   Insert into a_bkorders.order_details values(30822, 1, 1128,  10,  49.95);   

/* Apr 2012 */

Insert into a_bkorders.order_headers    values(30824, '2012-04-05', 222477);
   Insert into a_bkorders.order_details values(30824, 1, 1670, 10,  40.00);
   Insert into a_bkorders.order_details values(30824, 4, 2005, 20,  45.00);

Insert into a_bkorders.order_headers    values(2001,  '2012-04-02', 272787);
   Insert into a_bkorders.order_details values(2001, 1, 1448, 50,  25.00);

Insert into a_bkorders.order_headers    values(2002,  '2012-04-12', 272787);
   Insert into a_bkorders.order_details values(2002, 1, 1103,  20,  10.95);

Insert into a_bkorders.order_headers    values(2003,  '2012-04-12', 272787);
   Insert into a_bkorders.order_details values(2003, 1, 1103,  2,  12.00);

Insert into a_bkorders.order_headers    values(1564,  '2012-04-18', 227105);
   Insert into a_bkorders.order_details values(1564, 1, 1106,  50,  34.95);
   Insert into a_bkorders.order_details values(1564, 2, 1107,  50,  20.95);
   Insert into a_bkorders.order_details values(1564, 3, 2001,  50,  39.00);

Insert into a_bkorders.order_headers    values(1800,  '2012-04-12', 217796);
   Insert into a_bkorders.order_details values(1800, 1, 2009,  5,  34.95);
   Insert into a_bkorders.order_details values(1800, 2, 2008,  1,  46.95);

Insert into a_bkorders.order_headers    values(1801, '2012-04-13', 217796);
   Insert into a_bkorders.order_details values(1801, 1, 1103,  2,  10.95);
   Insert into a_bkorders.order_details values(1801, 2, 1106,  1,  29.00);

Insert into a_bkorders.order_headers    values(30825, '2012-04-21', 221297);
   Insert into a_bkorders.order_details values(30825, 1, 1776,  4,  45.49);

Insert into a_bkorders.order_headers    values(30826,  '2012-04-24', 211483);
   Insert into a_bkorders.order_details values(30826, 2, 1161,  16,  35.00);

Insert into a_bkorders.order_headers    values(30833,  '2012-04-14', 211483);
   Insert into a_bkorders.order_details values(30833, 1, 1448, 50,  25.00);

Insert into a_bkorders.order_headers    values(30834,  '2012-04-17', 211483);
   Insert into a_bkorders.order_details values(30834, 1, 1128,  1,  49.95);

/* May 2012 */

Insert into a_bkorders.order_headers    values(30835,  '2012-05-17', 211483);
   Insert into a_bkorders.order_details values(30835, 1, 1103,  25,  10.95);

Insert into a_bkorders.order_headers    values(30836,  '2012-05-20', 258595);
   Insert into a_bkorders.order_details values(30836, 1, 2008,  2,  12.50);

Insert into a_bkorders.order_headers    values(1811,  '2012-05-12', 221297);
   Insert into a_bkorders.order_details values(1811, 1, 2007,  1,  40.49);
   Insert into a_bkorders.order_details values(1811, 2, 1357,  2,  23.40);
   Insert into a_bkorders.order_details values(1811, 3, 1537,  3,  28.19);

Insert into a_bkorders.order_headers    values(1812,  '2012-05-12', 227105);
   Insert into a_bkorders.order_details values(1812, 1, 2009,  1,  26.99);

Insert into a_bkorders.order_headers    values(1814,  '2012-05-15', 290298);
   Insert into a_bkorders.order_details values(1814, 1, 1258,  1,  45.99);

Insert into a_bkorders.order_headers    values(1818,  '2012-05-16', 212921);
   Insert into a_bkorders.order_details values(1818, 1, 1106, 30,  20.00);
   Insert into a_bkorders.order_details values(1818, 2, 1537,  2,  25.00);
   Insert into a_bkorders.order_details values(1818, 3, 1180,  1,  46.99);
   Insert into a_bkorders.order_details values(1818, 4, 1979,  1,  53.99);

Insert into a_bkorders.order_headers    values(1710,  '2012-05-08', 261502);
   Insert into a_bkorders.order_details values(1710, 1, 1776,  99,  45.49);

Insert into a_bkorders.order_headers    values(1712,  '2012-05-09', 290298);
   Insert into a_bkorders.order_details values(1712, 1, 1835,  1,  45.99);
   Insert into a_bkorders.order_details values(1712, 2, 1162,  99,  30.00);

Insert into a_bkorders.order_headers    values(2004,  '2012-05-22', 272787);
   Insert into a_bkorders.order_details values(2004, 2, 1161,  1,  35.00);

Insert into a_bkorders.order_headers    values(2005,  '2012-05-30', 272787);
   Insert into a_bkorders.order_details values(2005, 1, 1448, 50,  25.00); 

/* June 2012 */

Insert into a_bkorders.order_headers    values(2012,  '2012-06-22', 272787);
   Insert into a_bkorders.order_details values(2012, 1, 1448, 50,  25.00);

Insert into a_bkorders.order_headers    values(2013,  '2012-06-22', 272787);
   Insert into a_bkorders.order_details values(2013, 1, 2009,  2,  12.50);

Insert into a_bkorders.order_headers    values(30847,  '2012-06-20', 296598);
   Insert into a_bkorders.order_details values(30847, 1, 1103,  2,  12.00);

Insert into a_bkorders.order_headers    values(30848,  '2012-06-21', 263119);
   Insert into a_bkorders.order_details values(30848, 1, 2007,  2,  12.50);

Insert into a_bkorders.order_headers    values(30849,  '2012-06-22', 217796);
   Insert into a_bkorders.order_details values(30849, 1, 1448, 50,  25.00);

/* July 2012 */   

Insert into a_bkorders.order_headers    values(31840,  '2012-07-01', 267780);
   Insert into a_bkorders.order_details values(31840, 1, 1103,  2,  12.00);

Insert into a_bkorders.order_headers    values(31841,  '2012-07-02', 272787);
   Insert into a_bkorders.order_details values(31841, 1, 1448, 50,  25.00);

Insert into a_bkorders.order_headers    values(31850,  '2012-07-02', 234138);
   Insert into a_bkorders.order_details values(31850, 1, 1279,  1,  40.49);

Insert into a_bkorders.order_headers    values(1045,  '2012-07-18', 222477);
   Insert into a_bkorders.order_details values(1045, 1, 1894,  1,  35.99);   

Insert into a_bkorders.order_headers    values(1200,  '2012-07-18', 212921);
   Insert into a_bkorders.order_details values(1200, 1, 1200,  5,  16.33);
   Insert into a_bkorders.order_details values(1200, 2, 1199,  5,  18.39);
   Insert into a_bkorders.order_details values(1200, 3, 1457,  5,  53.99);
   Insert into a_bkorders.order_details values(1200, 4, 1133,  5,  18.15);
   Insert into a_bkorders.order_details values(1200, 5, 1894,  5,  36.79);
   Insert into a_bkorders.order_details values(1200, 6, 1948,  5,  40.94);
   Insert into a_bkorders.order_details values(1200, 7, 1180,  5,  45.99);
   Insert into a_bkorders.order_details values(1200, 8, 1128,  5,  46.20);

Insert into a_bkorders.order_headers    values(1205,  '2012-07-20', 212921);
   Insert into a_bkorders.order_details values(1205, 1, 1448,  1,  27.29);

Insert into a_bkorders.order_headers    values(1212,  '2012-07-20', 290298);
   Insert into a_bkorders.order_details values(1212, 1, 1894,  1,  37.59);
   Insert into a_bkorders.order_details values(1212, 2, 1894,  1,  18.75);

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT p.publ_id, p.publ_name, oh.order_date
FROM a_bkinfo.publishers p 
JOIN a_bkinfo.books b USING (publ_id)
JOIN a_bkinfo.book_topics bt USING (book_id)
JOIN a_bkorders.order_details od USING (book_id)
JOIN a_bkorders.order_headers oh USING (order_id)
WHERE bt.topic_id IN ('SQL', 'DB') AND YEAR(oh.order_date) >= 2005 
GROUP BY p.publ_id HAVING COUNT(DISTINCT bt.topic_id) = 2;

答案 1 :(得分:0)

首先是警告。我认为这是我写过的第一个MySQL查询!所以我可能没有正确的语法。你加入的方式对我来说很奇怪。根据我的经验,对于什么和什么领域的联系,联接更加明确。我也不确定在SELECT子句中包含publ_name和order_date但在GROUP BY中不包括。无论GROUP BY子句如何,这会为每个发布者产生多个结果吗?

Saharsh的方法更好,但替代方案可能看起来像这样:

SELECT 
  p.publ_id, 
  max(p.publ_name) as publ_name, 
  max(oh.order_date) as order_date
FROM 
  a_bkinfo.publishers p INNER JOIN a_bkinfo.books b 
    ON p.publ_id = b.publ_id
  INNER JOIN a_bkinfo.book_topics bt 
    ON b.book_id = bt.book_id
  INNER JOIN a_bkinfo.book_topics bt2
    ON b.book_id = bt2.book_id
  INNER JOIN a_bkorders.order_details od 
    ON b.book_id = od.book_id
  INNER JOIN a_bkorders.order_headers oh 
    ON od.order_id = oh.order_id
WHERE 
  bt.topic_id = 'SQL' AND 
  bt2.topic_id = 'DB' AND 
  YEAR(oh.order_date) >= 2005 
GROUP BY 
  p.publ_id