遇到 Unknown column ‘rownum’ in ‘where clause’的解决方案

日常记录遇到的bug!!!
Unknown column ‘clickNumber’’ in ‘where clause’,当在嵌套的查询语句里面用到clickNumber的时候会遇到这个问题
首先我的语句是这样的
因为是统计语句,语句可能有点长,只需要看clickNumber出代码就可以
select t2.class_mark classMark ,t2.name className ,count(t2.class_mark) ebookNumber ,ifnull(t3.cnt, 0) clickNumber from ep_book t1
inner join ep_book_class t2 on t1.classmark_zjt = t2.class_mark LEFT JOIN ( SELECT t1.classmark_zjt classMark, ifnull( count( t2.book_id ), 0 ) cnt FROM ep_book t1
INNER JOIN ep_place t3 ON t3.id = t1.place_id INNER JOIN ep_statistics_click t2 ON t2.book_id = t1.id where 1=1 and t2.oid = 63 GROUP BY t1.classmark_zjt ) t3 ON t3.classMark = t1.classmark_zjt
where (clickNumber+ebookNumber)!=0 GROUP BY t2.class_mark ORDER BY clickNumber desc LIMIT 10

在执行这条语句的时候
会报如下错误
1054 - Unknown column ‘clickNumber’ in 'where clause’
这个是因为在嵌套的查询语句中,当用到where的时候,数据的条数可能还没有被决定, clickNumber这是根本都还没出来。所以会报错。
mysql官方的链接如下:http://bugs.mysql.com/bug.php?id=38879

这种情况只能是修改语句,修改语句如下
select * (select t2.class_mark classMark ,t2.name className ,count(t2.class_mark) ebookNumber ,ifnull(t3.cnt, 0) clickNumber from ep_book t1
inner join ep_book_class t2 on t1.classmark_zjt = t2.class_mark LEFT JOIN ( SELECT t1.classmark_zjt classMark, ifnull( count( t2.book_id ), 0 ) cnt FROM ep_book t1
INNER JOIN ep_place t3 ON t3.id = t1.place_id INNER JOIN ep_statistics_click t2 ON t2.book_id = t1.id where 1=1 and t2.oid = 63 GROUP BY t1.classmark_zjt ) t3 ON t3.classMark = t1.classmark_zjt
GROUP BY t2.class_mark ORDER BY clickNumber desc LIMIT 10) a where (clickNumber+ebookNumber)!=0
这样在他外面嵌套一个就可以完美解决了!!!

你可能感兴趣的:(Java)