【问题标题】:Can Some one please help me solve this subquery in mysql?有人可以帮我解决mysql中的这个子查询吗?
【发布时间】:2013-11-03 03:17:11
【问题描述】:

***规则: --- 不要使用任何类型的连接,也不要使用相关子查询。 --- 不要使用逗号连接。 --- 如果您使用加入,您将不会获得该任务的积分。 From 子句将仅引用一个表。 --- 不要使用变量。

***这是我的问题: 显示某人订购的任何书籍的书籍 ID 和标题,并且该书籍既是 SQL 书籍又是数据库书籍。使用 Topic_id 过滤 DB 和 SQL。按 book_id 排序。

***我的回答:

 select book_id, title
  From a_bkinfo.books
  Where 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 topic_id = 'SQL')
     order by book_id; 

我在这个问题上收到 -2。我的老师回复:-2 * 没有检查这本书是否已被订购。

***以下是相关表格。* 表格:

 ---- 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;

---- 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 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;

【问题讨论】:

    标签: mysql logic subquery logical-operators


    【解决方案1】:
    and book_id in 
        ( select d.book_id 
            from a_bkorders.order_details d
           where d.quantity > 0 
        )
    

    显示销量最多的书籍的ID和书名;包括关系。对于此查询,使用已售总数量。在确定一本书的销量时

    没有任何 JOIN 操作或相关子查询,这将返回指定的结果集:

    SELECT b.book_id
         , b.title
      FROM a_bkinfo.books b
     WHERE b.book_id IN
           ( 
             SELECT d.book_id
               FROM a_bkorders.order_details d
              GROUP BY d.book_id
             HAVING SUM(d.quantity) =
                    (
                      SELECT MAX(t.tot_quantity)
                        FROM (  
                               SELECT s.book_id
                                    , SUM(s.quantity) AS tot_quantity
                                 FROM a_bkorders.order_details s
                                GROUP BY s.book_id
                             ) t
                    )
           )
    

    注意 MySQL 强制执行 CHECK 约束。

    【讨论】:

    • 我就是这么想的。使用相同的表如何解决这个问题。显示销量最多的书籍的ID和书名;包括关系。对于此查询,使用已售总数量。在确定一本书的销量时。
    猜你喜欢
    • 2018-08-13
    • 2015-05-31
    • 2016-07-09
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多