ionik Posted October 15, 2008 Share Posted October 15, 2008 Ok I have a complex mysql statement....what it is supposed to do is select all users from the database, then for each user select all videos that they have related to their Id then from the videos get the total number of views, avg rating etc etc, the query runs fine but the problem is is that when it runs through and starts getting the video information it only gets information from just one video in the Database....and not all of them...as some users will return with 23 videos and the tracking data is only for the 23rd video.....any reason why this would happen? SELECT u.userName, u.userId, v.videoCount, c.commentCount, e.producerEarnings, w.viewCount, r.averageRating FROM user u LEFT JOIN ( SELECT videoId, userId, COUNT(*) AS videoCount FROM videos GROUP BY userId ) AS v USING (userId) LEFT JOIN ( SELECT videoId, COUNT(*) AS commentCount FROM videoComments GROUP BY videoId ) AS c USING (videoId) LEFT JOIN ( SELECT videoId, COUNT(*) AS viewCount FROM videoTracking GROUP BY videoId ) AS w USING (videoId) LEFT JOIN ( SELECT videoId, SUM(amount) AS producerEarnings FROM videoEarnings GROUP BY videoId ) AS e USING (videoId) LEFT JOIN ( SELECT videoId, ROUND( AVG(rating), 2 ) AS averageRating FROM videoRatings GROUP BY videoId ) AS r USING (videoId) WHERE userId < 100 ORDER BY u.userName DESC LIMIT 0,10 Quote Link to comment https://forums.phpfreaks.com/topic/128561-complex-join/ Share on other sites More sharing options...
fenway Posted October 15, 2008 Share Posted October 15, 2008 It's complex because it's all on one line... could you format that nicely? Quote Link to comment https://forums.phpfreaks.com/topic/128561-complex-join/#findComment-666371 Share on other sites More sharing options...
ionik Posted October 15, 2008 Author Share Posted October 15, 2008 SELECT u.userName, u.userId, v.videoCount, c.commentCount, e.producerEarnings, w.viewCount, r.averageRating FROM user u LEFT JOIN ( SELECT videoId, userId, COUNT(*) AS videoCount FROM videos GROUP BY userId ) AS v USING (userId) LEFT JOIN ( SELECT videoId, COUNT(*) AS commentCount FROM videoComments GROUP BY videoId ) AS c USING (videoId) LEFT JOIN ( SELECT videoId, COUNT(*) AS viewCount FROM videoTracking GROUP BY videoId ) AS w USING (videoId) LEFT JOIN ( SELECT videoId, SUM(amount) AS producerEarnings FROM videoEarnings GROUP BY videoId ) AS e USING (videoId) LEFT JOIN ( SELECT videoId, ROUND( AVG(rating), 2 ) AS averageRating FROM videoRatings GROUP BY videoId ) AS r USING (videoId) WHERE userId < 100 ORDER BY u.userName DESC LIMIT 0,10 Quote Link to comment https://forums.phpfreaks.com/topic/128561-complex-join/#findComment-666374 Share on other sites More sharing options...
ionik Posted October 15, 2008 Author Share Posted October 15, 2008 SELECT u.userName, u.userId, v.videoCount, c.commentCount, e.producerEarnings, w.viewCount, r.averageRating FROM user u LEFT JOIN ( SELECT videoId, userId, COUNT(*) AS videoCount FROM videos GROUP BY userId ) AS v USING (userId) LEFT JOIN ( SELECT videoId, COUNT(*) AS commentCount FROM videoComments GROUP BY videoId ) AS c USING (videoId) LEFT JOIN ( SELECT videoId, COUNT(*) AS viewCount FROM videoTracking GROUP BY videoId ) AS w USING (videoId) LEFT JOIN ( SELECT videoId, SUM(amount) AS producerEarnings FROM videoEarnings GROUP BY videoId ) AS e USING (videoId) LEFT JOIN ( SELECT videoId, ROUND( AVG(rating), 2 ) AS averageRating FROM videoRatings GROUP BY videoId ) AS r USING (videoId) WHERE userId < 100 ORDER BY u.userName DESC LIMIT 0,10 I have also tried creating th COUNT into a SUM using a sub query withing the subquery for each and producer the small result Quote Link to comment https://forums.phpfreaks.com/topic/128561-complex-join/#findComment-666376 Share on other sites More sharing options...
Barand Posted October 16, 2008 Share Posted October 16, 2008 In this subquery SELECT videoId, userId, COUNT(*) AS videoCount FROM videos GROUP BY userId you group by userid therefore you get one row for each user. Even if that user has 100 videos you can only get one video_id in that user's row. Quote Link to comment https://forums.phpfreaks.com/topic/128561-complex-join/#findComment-666854 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.