AlexanderTheGr Posted May 21, 2009 Share Posted May 21, 2009 Hi I have that query Select (sum(field1*field2)) as c from table where c > 100 group by field4 And i got that error Unknown column 'c' in 'where clause' Any idea how can i have a similar query Link to comment https://forums.phpfreaks.com/topic/159059-solved-sum-fields-in-where-clause/ Share on other sites More sharing options...
luca200 Posted May 21, 2009 Share Posted May 21, 2009 Not sure it'll work, but try this way Select (sum(field1*field2)) as c from table group by field4 having c > 100 Link to comment https://forums.phpfreaks.com/topic/159059-solved-sum-fields-in-where-clause/#findComment-838806 Share on other sites More sharing options...
GingerRobot Posted May 21, 2009 Share Posted May 21, 2009 Yeah, you cannot refer to an alias in the WHERE clause. Not entirely sure why. luca200's solution should work for you. Link to comment https://forums.phpfreaks.com/topic/159059-solved-sum-fields-in-where-clause/#findComment-838834 Share on other sites More sharing options...
AlexanderTheGr Posted May 21, 2009 Author Share Posted May 21, 2009 Thx luca Its seems its works Link to comment https://forums.phpfreaks.com/topic/159059-solved-sum-fields-in-where-clause/#findComment-838836 Share on other sites More sharing options...
luca200 Posted May 21, 2009 Share Posted May 21, 2009 Yeah, you cannot refer to an alias in the WHERE clause. Not entirely sure why. Because WHERE is executed BEFORE select, so it does'nt know aliases at the time. You have to repeat the expression if you have some. Anyway, in this case it was wrong at all to put the condition in the WHERE clause, even repeating the expression, because you can't test a condition on a grouped value in WHERE. Link to comment https://forums.phpfreaks.com/topic/159059-solved-sum-fields-in-where-clause/#findComment-838899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.