执行 GROUP BY 时强制将特定记录置顶

发布于 2024-09-24 00:42:48 字数 2796 浏览 0 评论 0原文

我有以下 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 字段应该属于登录用户(每个用户每首歌只能获得一个投票记录)。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-10-01 00:42:48

第三个连接

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

并将 Vote.value 替换为 VotePerUser.Value

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文