执行GROUP BY时强制特定记录到顶部

时间:2010-09-18 21:41:05

标签: php mysql sorting group-by sql-order-by

我有以下MySQL查询和表格,我正在查询:

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    |                | 
+----------+----------+------+-----+---------+----------------+

除了一件事之外,这个查询的功能就像我想要的一样。 Vote.value字段中返回的值不是与登录到应用程序的用户关联的行。我需要将得分值作为所有值的总和,无论它与哪个用户相关联,但Vote.value字段应该属于登录用户(每个用户每个歌曲只能获得一个投票记录)。

我的第一个想法是以某种方式对表进行排序,以便在发生组时,登录用户的投票记录位于顶部,但我不知道如何进行强制任意值到顶部的排序。任何想法都会非常有用。

1 个答案:

答案 0 :(得分:1)

和第三次加入

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

并将Vote.value替换为VotePerUser.Value