一个基本的 sql 查询示例

发布于 2024-12-24 19:55:11 字数 578 浏览 1 评论 0原文

我有两个表:

NEWS (id, news_content)
NEWS_VOTES (vote, news_id)

我应该选择 (*) NEWS 上的所有值,并对 NEWS_VOTES 表上的投票进行计数,其中 news.id 和 news_votes.new_id 相同。

更清楚一点的解释是:

  1. 我选择 NEWS 表上的所有值。
  2. 根据“id”值,我还选择:

负面投票:

SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 0

正面投票:

SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 1

我需要在单个查询中执行此操作。

网站上的输出将类似于“此消息获得 57 票赞成,67 票反对”。

谢谢。

诗。我用的是MYSQL。

I have two tables:

NEWS (id, news_content)
NEWS_VOTES (vote, news_id)

I should be selecting (*) all values on NEWS, and also count vote on NEWS_VOTES table where news.id and news_votes.new_id are the same.

A little clearer explanation is:

  1. I select all values on NEWS table.
  2. Depending on "id" value, I also select:

Negative Votes:

SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 0

Positive Votes:

SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 1

I need to do this in a single query.

The output on website will be like "This news got 57 positive votes and 67 negative votes."

Thank you.

Ps. I use MYSQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦纸 2024-12-31 19:55:11

假设赞成票有 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

assuming positive votes have 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
弱骨蛰伏 2024-12-31 19:55:11
SELECT n.id, n.news_content,
       COUNT(v1.vote) AS negative,
       COUNT(v2.vote) AS positive
FROM news n
LEFT JOIN news_votes v1 ON v1.news_id = n.id
LEFT JOIN news_votes v2 ON v2.news_id = n.id
HAVING v1.vote = 0 AND HAVING v2.vote = 1
GROUP BY v1.news_id, v2.news_id
ORDER BY id DESC
SELECT n.id, n.news_content,
       COUNT(v1.vote) AS negative,
       COUNT(v2.vote) AS positive
FROM news n
LEFT JOIN news_votes v1 ON v1.news_id = n.id
LEFT JOIN news_votes v2 ON v2.news_id = n.id
HAVING v1.vote = 0 AND HAVING v2.vote = 1
GROUP BY v1.news_id, v2.news_id
ORDER BY id DESC
花间憩 2024-12-31 19:55:11
select id, news_content, 
    coalesce(v.positive, 0) as positive, 
    coalesce(v.negative, 0) as negative
from news n
left join (
    select news_id, 
        sum(case when vote = 1 then 1 else 0 end) as positive,
        sum(case when vote = 0 then 1 else 0 end) as negative
    from news_votes
    group by news_id
) v on n.id = v.news_id
select id, news_content, 
    coalesce(v.positive, 0) as positive, 
    coalesce(v.negative, 0) as negative
from news n
left join (
    select news_id, 
        sum(case when vote = 1 then 1 else 0 end) as positive,
        sum(case when vote = 0 then 1 else 0 end) as negative
    from news_votes
    group by news_id
) v on n.id = v.news_id
凉城 2024-12-31 19:55:11

试试这个 -

SELECT
  n.*,
  COUNT(IF(nv.vote = 1, 1, NULL)) Positive_Votes,
  COUNT(IF(nv.vote = 0, 1, NULL)) Negative_Votes
FROM news n
  LEFT JOIN NEWS_VOTES nv
    ON nv.news_id = n.id
GROUP BY
  n.id

vote = 1 表示投赞成票, vote = 0 表示投反对票。

Try this one -

SELECT
  n.*,
  COUNT(IF(nv.vote = 1, 1, NULL)) Positive_Votes,
  COUNT(IF(nv.vote = 0, 1, NULL)) Negative_Votes
FROM news n
  LEFT JOIN NEWS_VOTES nv
    ON nv.news_id = n.id
GROUP BY
  n.id

Where vote = 1 means positive vote, and vote = 0 means negative vote.

梦情居士 2024-12-31 19:55:11

选择
(SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 0) Positive_votes,
(SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 1) negative_votes
来自双重;

select
(SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 0) positive_votes,
(SELECT count(*) FROM NEWS_VOTES WHERE news_id = (same ID) AND vote = 1) negative_votes
from dual;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文