如何从嵌套select中的外部查询中获取当前id

mrfwxfqh  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(316)

当我分组条目时,我试图从列中获取最常用的值。我想我得用一个 GROUP BY 为了让它工作。我有以下疑问:

SELECT post_id, (SELECT browser from visits WHERE
id = ? GROUP BY browser ORDER BY count(browser)
DESC limit 1) as common_browser, count(id) as visits FROM `visits` group by post_id

在“?”上,我想传递组的id,如果我向它传递一个参数,它将执行我尝试执行的操作,即: id = 1 . 如果有任何其他方法来完成这一点,请让我知道!

ioekq8ef

ioekq8ef1#

使用相关子查询。

SELECT post_id, (
    SELECT browser
    FROM visits AS v1
    WHERE v1.post_id = v.post_id
    GROUP by browser
    ORDER BY COUNT(*) DESC
    LIMIT 1) AS common_browser, COUNT(*) AS visits,
FROM visits AS v
GROUP BY post_id

相关问题