php - MySQL 查询根据得票最高的项目进行选择

标签 php mysql

我有一个简单的网络应用程序,当用户在图像上单击收藏夹时,数据库会存储一个 user_id 和他们正在查看的 image_id,表格如下所示:

Favorites
---------------------
-user_id - image_id -
---------------------
-abc     - 123      -
-abc     - 456      -
-def     - 123      -
---------------------

我正在尝试找出最喜欢的前 10 张图片(全局范围内),也就是总体上最受欢迎的 10 张图片。查询只需要找到最常出现的 10 个 image_id 值。到目前为止,我已经按照以下方式尝试了一些事情

SELECT image_id, COUNT(*) FROM favourites GROUP BY image_id LIMIT 100 ORDER DESC

完成此任务的正确查询是什么?

最佳答案

下面的查询应该可以解决问题,它与您的代码几乎相同,但最后一点不同:

select
    image_id,
    count(*)
from
    favourites
group by
    image_id
order by
    count(*) desc
limit 10

您可能还想阅读 Q&A that I wrote它涵盖了很多像这样的东西,非常深入。

编辑:

要回答下面的评论之一,在 order by 语句中使用 count(*) 会导致它再次计算吗?

没有。

mysql> select * from test2;
+------+-------+-------+
| id   | barry | third |
+------+-------+-------+
|    1 | ccc   |  NULL |
| NULL | d     |     1 |
| NULL | d     |     2 |
| NULL | d     |     3 |
+------+-------+-------+
4 rows in set (0.00 sec)

mysql> explain select barry, max(third) from test2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | test2 | ALL  | NULL          | NULL | NULL    | NULL |    4 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.11 sec)

mysql> explain select barry, max(third) from test2 order by barry;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | test2 | ALL  | NULL          | NULL | NULL    | NULL |    4 |       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)


mysql> explain select barry, max(third) from test2 order by max(third);
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra           |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------+
|  1 | SIMPLE      | test2 | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using temporary |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------+
1 row in set (0.00 sec)

从这里可以看出,它将数据存储在 temporary 中并从那里使用它。

关于php - MySQL 查询根据得票最高的项目进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18590178/

相关文章:

php - 无法在数据库中更新和/或插入图像(显示它也有问题)

php - 比较两个大表的唯一记录

php - 如何使用 PHP 创建搜索按钮

mysql 使用从数据库读取的列别名

mysql - 编写 MySQL 插件

mysql - 从数据库中有效地选择最近(距离)记录

php - 如何使用下拉列表将两个表连接到表上?

php mysql foreach循环重复结果

php - 使用 AJAX 生成 Dwolla 访问 token 而不是重定向到他们的网页?

php - 登录后带有phpmyadmin的Nginx错误重定向