mysql:按ID分组,每个ID获得最高优先级
我有一个名为“pics”的 mysql 表,具有以下字段和示例数据:
id vehicle_id filename priority
1 45 a.jpg 4
2 45 b.jpg 1
3 56 f.jpg 4
4 67 cc.jpg 4
5 45 kt.jpg 3
6 67 gg.jpg 1
是否可以在单个查询中为每个vehicle_id 获取一行,并且该行具有最高优先级?
我正在寻找的结果:
array (
[0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg', [priority] => '1' ),
[1] => array( [id] => '3', [vehicle_id] => '56', [filename] => 'f.jpg', [priority] => '4' ),
[2] => array( [id] => '6', [vehicle_id] => '67', [filename] => 'gg.jpg', [priority] => '1' )
);
如果在单个查询中不可能,那么最好的方法是什么?
谢谢!
I have the following mysql table called "pics", with the following fields and sample data:
id vehicle_id filename priority
1 45 a.jpg 4
2 45 b.jpg 1
3 56 f.jpg 4
4 67 cc.jpg 4
5 45 kt.jpg 3
6 67 gg.jpg 1
Is it possible, in a single query, to get one row for each vehicle_id, and the row be the highest priority?
The result I'm looking for:
array (
[0] => array( [id] => '2', [vehicle_id] => '45', [filename] => 'b.jpg', [priority] => '1' ),
[1] => array( [id] => '3', [vehicle_id] => '56', [filename] => 'f.jpg', [priority] => '4' ),
[2] => array( [id] => '6', [vehicle_id] => '67', [filename] => 'gg.jpg', [priority] => '1' )
);
If not possible in a single query, what would be the best approach?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然这可能是“已接受”的答案,但 Mark 的解决方案在正常情况下要好很多倍,并且对于这个问题同样有效,所以无论如何,在生产中采用他的解决方案!
假设“id”是不可为空的列。
[编辑]:我的错,需要第二次加入,不能只用一次。
While this may be the 'accepted' answer, the performance of Mark's solution is under normal circumstances many times better, and equally valid for the question, so by all means, go for his solution in production!
Assuming 'id' is a non-nullable column.
[edit]: my bad, need second join, cannot do it with just one.
这似乎是典型的每组最大查询。在大多数数据库中,您可以使用 ROW_NUMBER 轻松完成此操作:
由于 MySQL 尚不支持 ROW_NUMBER,您可以使用变量来模拟它:
This seems to be a typical max per group query. In most databases you could easily do this using ROW_NUMBER:
Since MySQL doesn't yet support ROW_NUMBER you can emulate it using variables:
请注意,此方法是在明确需要处理优先级关系之前发布的。我将其留在这里供参考(请参阅下面的评论)。查看@Mark 的回答 对于根据需要处理联系的解决方案:
这假设同一
vehicle_id
不能有优先级联系。测试用例:
结果:
Note that this method was posted before it was made clear that it is required to handle priority ties. I'm leaving it here for reference (see comments below). Check out @Mark's answer for a solution that handles ties as required:
This assumes that there can be no priority ties for the same
vehicle_id
.Test case:
Result: