newbtophp Posted September 18, 2010 Share Posted September 18, 2010 SELECT table1.title, table1.status FROM table1, table2 WHERE table1.author = '{$username}' AND COUNT(table2.id) > 0 AND table2.deleted = 0 But I get an error, but when I remove the ' AND COUNT(table2.id) > 0' the error goes; theirfore whats the correct way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/ Share on other sites More sharing options...
Pikachu2000 Posted September 18, 2010 Share Posted September 18, 2010 What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/#findComment-1112580 Share on other sites More sharing options...
kickstart Posted September 18, 2010 Share Posted September 18, 2010 Hi First problem is that you need to join the tables on a field. What you have is a cross join, and will bring back every combination of the rows from the 2 tables. As to checking an aggregate value, use a GROUP BY to group the records you want to count and use a HAVING clause to check the value. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/#findComment-1112594 Share on other sites More sharing options...
newbtophp Posted September 19, 2010 Author Share Posted September 19, 2010 Hi First problem is that you need to join the tables on a field. What you have is a cross join, and will bring back every combination of the rows from the 2 tables. As to checking an aggregate value, use a GROUP BY to group the records you want to count and use a HAVING clause to check the value. All the best Keith $pm_query = "SELECT table2.id FROM table1, table2 WHERE table2.id = table1.id AND table1.author = '{$username}'"; $pm_reply_count = mysql_num_rows(mysql_query($pm_query)); //final query (which shall be used within a while loop) $pm_id_query = "SELECT id FROM table1 WHERE (id = '{$id}' AND author = '{$username}' OR pm_author = '{$username}' AND {$pm_reply_count} > 0) AND deleted = 0"; I've come up with the above? but its kinda hackish? - their must be a better way I'm just unsure on how to Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/#findComment-1112628 Share on other sites More sharing options...
ignace Posted September 19, 2010 Share Posted September 19, 2010 You should explain what you are trying to do, helping without context is difficult as I am sure the below won't give you the desired output also note that using aggregate functions implies GROUP BY If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. SELECT table1.title, table1.status FROM table1, table2 WHERE table1.author = '{$username}' AND table2.deleted = 0 GROUP BY table2.id HAVING count(table2.id) > 0 Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/#findComment-1112728 Share on other sites More sharing options...
kickstart Posted September 19, 2010 Share Posted September 19, 2010 Hi You can merge them like this:- SELECT table1.id, COUNT(table2,id) FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table1.id = '{$id}' AND (table1.author = '{$username}' OR table1.pm_author = '{$username}' ) AND table1.deleted = 0 GROUP BY table1.id Not sure on checking author / pm_author and deleted, so you might need to move the brackets around depending on exactly what you want. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213760-where-counttable2id-0/#findComment-1112939 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.