我有两张桌子:
NEWS (id, news_content)
NEWS_VOTES (vote, news_id)
Run Code Online (Sandbox Code Playgroud)
我应该在NEWS上选择(*)所有值,并在NEWS_VOTES表上计算投票,其中news.id和news_votes.new_id是相同的.
更清楚的解释是:
负面投票:
SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 0
Run Code Online (Sandbox Code Playgroud)
正面投票:
SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 1
Run Code Online (Sandbox Code Playgroud)
我需要在一个查询中执行此操作.
网站上的输出将是"这条新闻得到57张正面投票和67张反对票".
谢谢.
PS.我用MYSQL.
假设正面投票有vote=1:
select
n.id,
n.news_content,
(select count(*) from news_votes where news_id=n.id and vote = 1) as positive_votes,
(select count(*) from news_votes where news_id=n.id and vote = 0) as negative_votes
from news n
Run Code Online (Sandbox Code Playgroud)