I have this query that I’m running against mysql:
SELECT video.title AS title,
video.url AS url,
video.id AS id,
video.description AS description,
avg(BodyPartsVideosUser.rating) AS rating
FROM videos AS video
LEFT JOIN body_parts_videos as BodyPartsVideo on BodyPartsVideo.video_id = video.id
LEFT JOIN body_parts_videos_users as BodyPartsVideosUser on BodyPartsVideo.id = BodyPartsVideosUser.body_parts_video_id
WHERE BodyPartsVideo.body_part_id = 39
GROUP BY BodyPartsVideosUser.body_parts_video_id
ORDER BY rating DESC ;
Now, the problem is that I get back one row with this query.
However, my videos table has three totally distinct entries, and my body_parts_videos table has three entries corresponding to the videos table. However, the body_part_id column there is 39 for each entry.
the body_parts_videos_users table is empty.
How do i alter this query so i get all three row returned?
body_parts_videos:
+----+--------------+----------+
| id | body_part_id | video_id |
+----+--------------+----------+
| 1 | 39 | 26 |
| 3 | 39 | 28 |
| 4 | 39 | 29 |
+----+--------------+----------+
videos:
+----+---------+-------------+--------------------------------------+-------------+----------
| id | title | description | user_id | url | filename | created | modified |
+----+---------+-------------+--------------------------------------+-------------+----------
| 26 | new vid | dd | 1 | eMvu0Cbi040 | a.avi | 2011-11-23 | 2011-11-23 |
| 28 | new vid | dd | 2 | ETyBso2SHJM | b.avi | 2011-11-23 | 2011-11-23 |
| 29 | title | ddd | 3 | 2Ud41iWKUIg | c.avi | 2011-11-23 | 2011-11-23 |
+----+---------+-------------+--------------------------------------+-------------+----------
body_parts_videos_users:
empty
I want to select from the empty body_parts_videos_users table because, when there are results, I want them to appear first. The empty row magically becomes zero somehow, but I’m fine with that.
Any ideas what I’m doing wrong? Oh! Here is what i get when i run the query:
+---------+-------------+----+-------------+--------+-
| title | url | id | description | rating |
+---------+-------------+----+-------------+--------+-
| new vid | eMvu0Cbi040 | 26 | dd | NULL |
+---------+-------------+----+-------------+--------+-
Here’s what i’d expect:
+---------+-------------+----+-------------+--------+-
| title | url | id | description | rating |
+---------+-------------+----+-------------+--------+-
| new vid | eMvu0Cbi040 | 26 | dd | NULL |
+---------+-------------+----+-------------+--------+-
| new vid | ETyBso2SHJM | 28 | dd | NULL |
+---------+-------------+----+-------------+--------+-
| title | 2Ud41iWKUIg | 29 | ddd | NULL |
+---------+-------------+----+-------------+--------+-
Any help is much appreciated!
I think the problem is, that your
body_parts_videos_userstable is empty.change your
GROUP BYtoGROUP BY body_parts_videos.body_part_id,body_parts_videos.video_id