mysql 查询以检查用户是否投票

mysql query to check if user voted

在这种情况下我有

posts table :

id 
title
description

post_votes table:

id
post_id
user_id

评论table:

id
post_id
user_id
content

用户table:

id
username

我查询我的索引页面(登录用户)

select 
`posts`.`id`, 
`posts`.`title`, 
`posts`.`description`,  
COUNT(distinct post_votes.id) AS votes, 
COUNT(distinct comments.id) AS comments 
from `posts` 
left join `post_votes` on `post_votes`.`post_id` = `posts`.`id` 
left join `comments` on `comments`.`post_id` = `posts`.`id` 
group by `posts`.`id`

这会显示所有 post(事件)。对于每一个显示总票数和评论数。此外,在索引页上为每个 post 显示一个投票按钮。

用户可以为一篇文章投票,我希望能够检查用户是否已经为每篇文章投票 post,在这种情况下,显示禁用了这篇 post 的投票按钮。

我有用户id。

我该怎么做?

不确定 mysql 格式,但像这样的格式应该可行:

select 
CASE WHEN userVote.post_id IS NULL THEN 'No user vote' ELSE 'User has voted' END AS 'VoteStatus'
`posts`.`id`, 
`posts`.`title`, 
`posts`.`description`,  
COUNT(distinct post_votes.id) AS votes, 
COUNT(distinct comments.id) AS comments 
from `posts` 
left join `post_votes` on `post_votes`.`post_id` = `posts`.`id`
left join `post_votes` AS userVote on `post_votes`.`post_id` = `posts`.`id` AND `post_votes`.`user_id` = @myUserID
left join `comments` on `comments`.`post_id` = `posts`.`id` 
group by `posts`.`id`