简体   繁体   English

MySQL:where子句中的未知列

[英]MySQL: Unknown column in where clause

SELECT B.*, SC.cate_name, (
        CASE WHEN special_offer_type ='Fixed Value'
        THEN B.price - special_offer
        WHEN special_offer_type =  'Discount %'
        THEN B.price * ( 1 - special_offer / 100.0 ) 
        ELSE B.price
        END
        ) AS final_price,
(IFNULL(xx.avg_rate,'0')) AS avg_rate,
(IFNULL(yy.count_comment, '0')) AS count_comment
FROM book B JOIN setting_category SC ON B.cate_id = SC.cate_id 
LEFT JOIN (SELECT a.isbn, sum(a.rate) / count(a.rate) AS avg_rate 
FROM book_rating a 
GROUP BY a.isbn) AS xx ON b.isbn = xx.isbn 
LEFT JOIN (SELECT c.isbn, count(*) AS count_comment FROM book_comment c 
GROUP BY c.isbn) AS yy ON b.isbn = yy.isbn

Used above coding, I added 4 columns cate_name , final_price , avg_rate and count_comment at result_table (THX Gordon Linoff, Ronald Alexander Kailola) 在上面的编码中,我在result_table添加了4列cate_namefinal_priceavg_ratecount_comment (THX Gordon Linoff,Ronald Alexander Kailola)

User AAA and BBB added rating to the book 001, so avg_rate of book 001 is (4+5)/2 = 4.5 用户AAA和BBB向图书001添加了评分,因此图书001的avg_rate为(4 + 5)/ 2 = 4.5

User XXX and YYY added comment to the book 001, so count_comment of book 001 = 2 用户XXX和YYY向书001添加了注释,因此书001的count_comment = 2

Now, I want to query the range of final_price added following code at the end. 现在,我想查询最后添加以下代码的final_price范围。

WHERE final_price BETWEEN '60' AND '100' ORDER BY final_price

However, I get an error #1054 - Unknown column 'final_price' in 'where clause' How can I fix it? 但是,我收到一个错误#1054 - Unknown column 'final_price' in 'where clause' 如何解决? and any suggestion to simplify the code? 还有简化代码的建议吗?

book
-----------------------------------------------------------                     
isbn   cate_id   price   special_offer   special_offer_type
001    1         125     5               Fixed Value
002    1         90      30              Discount %
003    2         150     50              Fixed Value

setting_category
--------------------                            
cate_id   cate_name
1         Fiction
2         Dictionary

book_rating                             
------------------------------------------
user   dateadd      timeadd    isbn   rate
AAA    2014/03/20   15:00:00   001     4
BBB    2014/03/21   15:00:00   001     5
CCC    2014/03/22   15:00:00   002     2

book_comment
----------------------------------------------
user    dateadd      timeadd    isbn   comment
XXX     2014/03/20   16:00:00   001    good
YYY     2014/03/21   16:00:00   001    great

result_table
-----------------------------------------------------------------------------------------------------------------
isbn   cate_id   price   special_offer   special_offer_type   cate_name    final_price   avg_rate   count_comment
001    1         125     5               Fixed Value          Fiction      120           4.5        2
002    1         90      30              Discount %           Fiction      63            2          0
003    2         150     50              Fixed Value          Dictionary   100           0          0

Embed having clause as like this: 像下面这样嵌入having子句:

-- include other part of your query here
FROM book B JOIN setting_category SC ON B.cate_id = SC.cate_id 
LEFT JOIN (SELECT a.isbn, sum(a.rate) / count(a.rate) AS avg_rate 
           FROM book_rating a 
           GROUP BY a.isbn) AS xx ON b.isbn = xx.isbn 
LEFT JOIN (SELECT c.isbn, count(*) AS count_comment FROM book_comment c 
           GROUP BY c.isbn) AS yy ON b.isbn = yy.isbn

-- having clause start
HAVING final_price BETWEEN '60' AND '100'
-- having end

ORDER BY final_price

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM