mysql: How to INNER JOIN a table but limit join to 1 result with the highest vote or count?

做~自己de王妃 提交于 2019-12-02 04:07:39
SELECT a.*, b.*
  FROM Items a
       LEFT JOIN Votes b on a.item_id = b.item_id
                         and b.total_yes = (select max(total_yes) 
                                              from Votes v 
                         where v.item_id = a.item_id)
ORDER BY a.post_date DESC, b.total_yes DESC

N.B.: if you have for an item 2 answers with the same total_yes = max, you will have 2 rows for that item.

add LIMIT 1 to the end of your query :)

that will take only one record, but at the moment you're ordering by the date first, so you'll get the highest vote for the last date voted on. Is that what you want?

If you want the highest total vote regardless you'll need to order by that first.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!