Sepph Posted October 31, 2009 Share Posted October 31, 2009 Hi, I need some help understanding subqueries. I've tried reading general documentation but it's not really helping. The best way I think I'll understand is by seeing the code for what I need a subquery. So, I want to display the number of users that have posted more than X photos in the past X days. The related tables with their relevant fields for this are users (id) and photos (id, user_id, created_at etc). Another subquery I'd need is to count all photos with more than X tags. tags and users are linked by a taggings table. The related tables of the db for this subquery would be users (id), tags (id), taggings (photo_id, tag_id). I'm used to doing queries with the sprintf function, using mysql_real_escape_string, so I'd best understand if these were used in the explanation. Any help would be appreciated, thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/ Share on other sites More sharing options...
Mchl Posted October 31, 2009 Share Posted October 31, 2009 These should work SELECT COUNT(*) AS userCount FROM ( SELECT user_id, COUNT(*) AS photoCount FROM photos WHERE created_at > DATE_ADD(NOW(), INTERVAL -? DAY) GROUP BY user_id HAVING photoCount > ? ) AS subQuery SELECT COUNT(*) AS photoCount FROM ( SELECT photo_id, COUNT(*) AS photoCount FROM taggings GROUP BY photo_id HAVING photoCount > ? ) AS subQuery Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948447 Share on other sites More sharing options...
Sepph Posted October 31, 2009 Author Share Posted October 31, 2009 They work like a charm, thanks a lot mate, never thought I actually don't even need the users or photos tables. Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948453 Share on other sites More sharing options...
Sepph Posted October 31, 2009 Author Share Posted October 31, 2009 BTW is it possible to do this with JOINs? Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948458 Share on other sites More sharing options...
Mchl Posted October 31, 2009 Share Posted October 31, 2009 Most likely, but it would be overcomplicating things. Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948461 Share on other sites More sharing options...
Sepph Posted October 31, 2009 Author Share Posted October 31, 2009 Alright, another question if I may... I want to count the photos with no votes. Problem is the votes table contains votes already given, so it doesn't contain photo id's for photos with no votes, which means I can't count them. I've just been using a separate table column with the vote cache, but is there a workaround inside the query? The votes table contains: id, photo_id, ip, created_at. Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948470 Share on other sites More sharing options...
Mchl Posted October 31, 2009 Share Posted October 31, 2009 For example like this SELECT COUNT(*) AS photoCount FROM photos WHERE id NOT IN (SELECT DISTINCT photo_id FROM votes) or SELECT COUNT(*) AS photoCount FROM photos AS p LEFT JOIN votes AS v ON (p.id = v.photo_id) WHERE v.photo_id IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948471 Share on other sites More sharing options...
Sepph Posted October 31, 2009 Author Share Posted October 31, 2009 Thanks again, they both work great Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948474 Share on other sites More sharing options...
Sepph Posted November 1, 2009 Author Share Posted November 1, 2009 Bumped into another problem... Trying to select users that have a minimum of X photos with a minimum of Y votes. This is what my query looks like: $query = sprintf("SELECT firstname, lastname FROM users, photos WHERE (SELECT COUNT(*) AS photoCount FROM photos WHERE (SELECT COUNT(*) AS votesCount FROM votes, photos WHERE votes.photo_id=photos.id ) >= '%s' ) >= '%s' AND users.id = photos.user_id", mysql_real_escape_string($votes), mysql_real_escape_string($photos) ); Not getting the right results... Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948501 Share on other sites More sharing options...
Sepph Posted November 1, 2009 Author Share Posted November 1, 2009 Still haven't managed to get this to work. Any help as to how to link everything together would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948686 Share on other sites More sharing options...
Mchl Posted November 1, 2009 Share Posted November 1, 2009 Ok... so first let's try to select photos with min Y votes SELECT photo_id, COUNT(*) AS voteCount FROM votes GROUP BY photo_id HAVING voteCount > ? Now let's try to select users that have at least X of these photos SELECT user_id, COUNT(*) AS photoCount FROM photos AS p INNER JOIN ( -- here put the query above ) AS votesSubQuery ON (votesSubQuery.photo_id = p.id) GROUP BY user_id HAVING photoCount > ? At last, let's select their first and last names SELECT firstname, lastname FROM users AS u INNER JOIN ( -- here put the query above ) AS photosSubQuery ON (photosSubQuery.user_id = u.ID) Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948700 Share on other sites More sharing options...
Sepph Posted November 1, 2009 Author Share Posted November 1, 2009 Thanks yet again! Quote Link to comment https://forums.phpfreaks.com/topic/179766-solved-subquery-help/#findComment-948722 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.