imgrooot Posted May 9, 2019 Share Posted May 9, 2019 I have a simply query where I want show how many unique contests are there. So if I have 100 entries in a single contest, then each entry row will have the same contest id in this example. So even though there are 100 entries, they all belong to the same contest id. That means the echo of $total_contests should be 1. But instead I am getting the same count as the entry rows, which is 100. What am I doing wrong? $global_user_id = 1; $count_contests = $db->prepare("SELECT COUNT(*) FROM entries WHERE user_id = :user_id GROUP BY contest_id"); $count_contests->bindParam(':user_id', $global_user_id); $count_contests->execute(); $total_contests = $count_contests->fetchColumn(); echo $total_contests; Quote Link to comment https://forums.phpfreaks.com/topic/308684-quick-help-this-group-by-query-not-working-properly/ Share on other sites More sharing options...
Zane Posted May 9, 2019 Share Posted May 9, 2019 You need to include the contest_id in your SELECT clause. SELECT contest_id, COUNT(*) FROM entries WHERE user_id = :user_id GROUP BY contest_id Quote Link to comment https://forums.phpfreaks.com/topic/308684-quick-help-this-group-by-query-not-working-properly/#findComment-1566554 Share on other sites More sharing options...
imgrooot Posted May 9, 2019 Author Share Posted May 9, 2019 7 minutes ago, Zane said: You need to include the contest_id in your SELECT clause. SELECT contest_id, COUNT(*) FROM entries WHERE user_id = :user_id GROUP BY contest_id Well...that was simple and a quick fix. Works now. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308684-quick-help-this-group-by-query-not-working-properly/#findComment-1566555 Share on other sites More sharing options...
Barand Posted May 10, 2019 Share Posted May 10, 2019 That will give the number of entries in each contest. For the number of contests SELECT COUNT(DISTINCT contest_id) as contests FROM entries WHERE user_id = :user_id or, for all users SELECT user_id , COUNT(DISTINCT contest_id) as contests FROM entries GROUP BY user_id Quote Link to comment https://forums.phpfreaks.com/topic/308684-quick-help-this-group-by-query-not-working-properly/#findComment-1566562 Share on other sites More sharing options...
imgrooot Posted May 11, 2019 Author Share Posted May 11, 2019 21 hours ago, Barand said: That will give the number of entries in each contest. For the number of contests SELECT COUNT(DISTINCT contest_id) as contests FROM entries WHERE user_id = :user_id or, for all users SELECT user_id , COUNT(DISTINCT contest_id) as contests FROM entries GROUP BY user_id Ah yes this is a better solution. I've tested it and it works. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/308684-quick-help-this-group-by-query-not-working-properly/#findComment-1566597 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.