gpt4 book ai didi

php - 按不同表中的数据对mysql表进行排序

转载 作者:行者123 更新时间:2023-11-30 01:17:23 24 4
gpt4 key购买 nike

我必须mysql表并尝试显示table1的结果但按table2排序。对于 table2,我正在计算重复 id 的所有出现次数,然后按降序显示该结果。以下是我所能得到的,想知道这是否可以通过单个查询来完成。

$query = "
SELECT DISTINCT registration.*

FROM registration

INNER JOIN downloads
ON registration.id = downloads.id

GROUP BY downloads.COUNT(id)
ORDER BY downloads.COUNT(id) DESC,
downloads.COUNT(id) DESC

";

最佳答案

认为你想要这样的东西:

SELECT Registration.id, Registration.name, Registration.email,
Downloads.document
FROM Registration
JOIN Downloads
ON Downloads.id = Registration.id
JOIN (SELECT id, COUNT(*) as count
FROM Downloads
GROUP BY id) Download_Count
ON Download_Count.id = Registration.id
ORDER BY Download_Count.count DESC

(未经测试,因为OP在问题中提供示例数据和表格布局会很好)

关于php - 按不同表中的数据对mysql表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com