如何计算同一列中不同where子句的值?

时间:2023-01-19 07:58:58

I hate that I have to ask but I just cant handle it.

我讨厌我不得不问,但我无法处理它。

I have this table votes:

我有这张桌子票:

如何计算同一列中不同where子句的值?

type=1 is an upvote, type=0 would be a downvote

type = 1是upvote,type = 0将是downvote

I want this output:

我想要这个输出:

[
  {'video': 'best-of-mehrwert', 'upvote': 2, 'downvote': 0},
  {...}
]

I am using medoo:

我正在使用medoo:

<?php
$votes = $database->query(
  // 'SELECT COUNT(video) as votes, video FROM votes GROUP BY video, type'
  '
    SELECT video,COUNT(*) as counts
    FROM votes
    GROUP BY video,type;

  '
)->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($votes);

which gives me

这给了我

[{"video":"anlaesse","counts":"1"},{"video":"best-of-mehrwert","counts":"2"}]

How do I "add a column" like "upvotes" i.e. entries where type = 1 and the same with type = 0?

如何“添加一个列”,如“upvotes”,即type = 1和type = 0的条目?

3 个解决方案

#1


1  

I think it might be easiest to SUM up a 1 for each row where your criteria matches:

我认为,对于您的条件匹配的每一行,最简单的方法是:

SELECT 
    video, 
    SUM(CASE type WHEN 1 THEN 1 ELSE 0 END) as upvotes,
    SUM(CASE type WHEN 0 THEN 1 ELSE 0 END) as downvotes 
FROM 
    votes
GROUP BY 
    video;

Note, you should omit type from the GROUP BY in order to get a single row back for each video.

注意,您应该省略GROUP BY中的类型,以便为每个视频返回一行。

#2


2  

two variants:

两种变体:

select
  video,
  sum(case when type=1 then 1 else 0 end) as upvote,
  sum(case when type=0 then 1 else 0 end) as downvote
from votes
group by video

and

select
  video,
  sum(type) as upvote,
  sum(1-type) as downvote
from votes
group by video

http://www.sqlfiddle.com/#!9/c73f2a/5

http://www.sqlfiddle.com/#!9/c73f2a/5

#3


0  

You could use case expression to count only the type of votes you're interested in:

您可以使用案例表达式仅计算您感兴趣的投票类型:

SELECT   video, 
         COUNT(CASE type WHEN 1 THEN 1 END) as upvotes
         COUNT(CASE type WHEN 0 THEN 1 END) as downvotes
FROM     votes
GROUP BY video, type;

#1


1  

I think it might be easiest to SUM up a 1 for each row where your criteria matches:

我认为,对于您的条件匹配的每一行,最简单的方法是:

SELECT 
    video, 
    SUM(CASE type WHEN 1 THEN 1 ELSE 0 END) as upvotes,
    SUM(CASE type WHEN 0 THEN 1 ELSE 0 END) as downvotes 
FROM 
    votes
GROUP BY 
    video;

Note, you should omit type from the GROUP BY in order to get a single row back for each video.

注意,您应该省略GROUP BY中的类型,以便为每个视频返回一行。

#2


2  

two variants:

两种变体:

select
  video,
  sum(case when type=1 then 1 else 0 end) as upvote,
  sum(case when type=0 then 1 else 0 end) as downvote
from votes
group by video

and

select
  video,
  sum(type) as upvote,
  sum(1-type) as downvote
from votes
group by video

http://www.sqlfiddle.com/#!9/c73f2a/5

http://www.sqlfiddle.com/#!9/c73f2a/5

#3


0  

You could use case expression to count only the type of votes you're interested in:

您可以使用案例表达式仅计算您感兴趣的投票类型:

SELECT   video, 
         COUNT(CASE type WHEN 1 THEN 1 END) as upvotes
         COUNT(CASE type WHEN 0 THEN 1 END) as downvotes
FROM     votes
GROUP BY video, type;