johnrb87 Posted November 2, 2010 Share Posted November 2, 2010 Hi All I have the following QUERY SELECT my.id, my.position, my.date, my.active, (SELECT COUNT(*) FROM `stuff` WHERE user = my.id) AS total_count FROM staff my WHERE my.position != 99 AND total_count = 0 ORDER BY my.id ASC it works great, apart from the part AND total_count = 0 which causes an error. Why can it not do this? Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/217577-count-in-select-query-issue/ Share on other sites More sharing options...
mikosiko Posted November 2, 2010 Share Posted November 2, 2010 my.total_count Quote Link to comment https://forums.phpfreaks.com/topic/217577-count-in-select-query-issue/#findComment-1129523 Share on other sites More sharing options...
johnrb87 Posted November 2, 2010 Author Share Posted November 2, 2010 my.total_count I get a #1054 - Unknown column 'my.total_count' in 'where clause' error Quote Link to comment https://forums.phpfreaks.com/topic/217577-count-in-select-query-issue/#findComment-1129529 Share on other sites More sharing options...
mikosiko Posted November 2, 2010 Share Posted November 2, 2010 my bad... if my memory doesn't fail you can't use a subquery alias in a where clause (I couldn't find the specific paragraph in the manual) this is one way to solve it: SELECT my.id, my.position, my.date, my.active, (SELECT COUNT(*) FROM `stuff` WHERE user = my.id) AS total_count FROM staff my WHERE my.position != 99 HAVING total_count = 0 ORDER BY my.id ASC Quote Link to comment https://forums.phpfreaks.com/topic/217577-count-in-select-query-issue/#findComment-1129553 Share on other sites More sharing options...
johnrb87 Posted November 2, 2010 Author Share Posted November 2, 2010 nice 1, thanks mikosiko Quote Link to comment https://forums.phpfreaks.com/topic/217577-count-in-select-query-issue/#findComment-1129556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.