0

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

***这是我的问题:对于书名中包含 Bird 的书表中的每本书,显示书名和书名以及关于我们是否有该书的订单的消息。不要使用 Count() 函数。输出将遵循此格式,并按 OrderStatus 列排序,其中没有顺序的书籍优先;第二个排序键是书籍 ID。

这是使用 count() 函数和相关子查询的解决方案。如何解决这个问题?请记住,我也不能使用联接。

SELECT book_id, title 
, CASE (
 SELECT count(*)
 FROM a_bkorders.order_details OD
 WHERE OD.book_id = BK.book_id
   )
 when 0 then 'no orders' 
 when 1 then 'Have orders'        
 when 2 then 'Have orders'   
 when 3 then 'Have orders'        
 else  'Have orders'        
 End as OrderStatus
From a_bkinfo.books BK
WHERE title Like '%Bird%'
ORDER BY OrderStatus, book_id;
4

2 回答 2

0

SELECT BookId,Title, COUNT(BookId),(IF (COUNT(BookId))>0,"No Order","Have Orders") AS Status FROM a_bkinfo.books BK WHERE title LIKE '%Bird%' GROUP BY BookId ORDER BY book_id

于 2013-11-04T10:24:07.600 回答
0

试试这个查询:

SELECT book_id, title 
, CASE when (SELECT count(*) FROM a_bkorders.order_details OD WHERE OD.book_id = BK.book_id) = 0
then 'no orders' 
       when (SELECT count(*) FROM a_bkorders.order_details OD WHERE OD.book_id = BK.book_id) = 1 
then 'Have orders'        
       when (SELECT count(*) FROM a_bkorders.order_details OD WHERE OD.book_id = BK.book_id) = 2
then 'Have orders'   
       when (SELECT count(*) FROM a_bkorders.order_details OD WHERE OD.book_id = BK.book_id) = 3 
then 'Have orders'        
else  'Have orders'        
End as OrderStatus
From a_bkinfo.books BK
WHERE title Like '%Bird%'
ORDER BY OrderStatus, book_id;
于 2013-11-04T11:47:15.440 回答