views:

35

answers:

1

I have the following MySQL query and tables from which I am querying:

SELECT
 `Song`.`id`,
 `Song`.`file_name`,
 `User`.`first_name`,
 `Vote`.`value`,
 Sum(`Vote`.`value`) AS score
FROM `songs` AS `Song`
LEFT JOIN votes AS `Vote` ON (`Song`.`id`=`Vote`.`song_id`)
LEFT JOIN `users` AS `User` ON (`Song`.`user_id` = `User`.`id`)
GROUP BY `Vote`.`song_id`
LIMIT 20;

mysql> describe songs;
+-----------+--------------+------+-----+---------+----------------+
| Field     | Type         | Null | Key | Default | Extra          |
+-----------+--------------+------+-----+---------+----------------+
| id        | int(11)      | NO   | PRI | NULL    | auto_increment | 
| file_name | varchar(255) | NO   |     | NULL    |                | 
| user_id   | int(11)      | NO   |     | NULL    |                | 
| created   | datetime     | NO   |     | NULL    |                | 
| modified  | datetime     | NO   |     | NULL    |                | 
+-----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> describe users;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment | 
| username   | varchar(255) | NO   |     | NULL    |                | 
| password   | varchar(255) | NO   |     | NULL    |                | 
| first_name | varchar(255) | NO   |     | NULL    |                | 
| last_name  | varchar(255) | NO   |     | NULL    |                | 
| is_admin   | tinyint(1)   | NO   |     | 0       |                | 
| created    | datetime     | NO   |     | NULL    |                | 
| modified   | datetime     | NO   |     | NULL    |                | 
+------------+--------------+------+-----+---------+----------------+

mysql> describe votes;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment | 
| value    | int(11)  | NO   |     | NULL    |                | 
| song_id  | int(11)  | NO   |     | NULL    |                | 
| user_id  | int(11)  | NO   |     | NULL    |                | 
| created  | datetime | NO   |     | NULL    |                | 
| modified | datetime | NO   |     | NULL    |                | 
+----------+----------+------+-----+---------+----------------+

This query functions just like I want except for one thing. The value returned in the field Vote.value is not from a row that is associated with the user who is logged into the application. I need the score value to be a sum of all the values no matter which user it is associated with, but the Vote.value field should belong to the logged in user (each user only gets one vote record per song).

My first thought is to somehow sort the table so that when the group by happens the vote record for the logged in user is at the top but I have no idea how to do a sort that forces an arbitrary value to the top. Any ideas would be very helpful.

+1  A: 

and a third join

LEFT JOIN votes AS `VotePerUser` ON (`Song`.`id`=`Vote`.`song_id` 
AND `Song`.`user_id`=`votes`.`user_id`)

and replace the Vote.value with VotePerUser.Value

Sparky
I had solved it with a sub-query and then came back to check and saw your solution. I like yours a lot better! Thanks!
macinjosh