【问题标题】:How to count and get result of two rows in Mysql Select Query?如何在 Mysql Select Query 中计算并获取两行的结果?
【发布时间】:2026-02-15 17:35:02
【问题描述】:

这是我的表结构。 有时 table1 数据我重复(例如:实际上 Id 1 应该只有 4 行,但有时它是 8 由于重复)所以避免重复我在选择查询中使用 GROUP BY 命令

表 1

|id| website|time|
-----------------
|01|facebook|20.0|
|01|google  |40.0|
|01|youtube |10.0|
|01|ebay    |30.0|
|02|facebook|50.0|
|02|ebay    |50.0|

表 2

    |id|marks|
    ----------- 
    |01|   80|
    |02|   90|
    |03|   70|
    |04|  100|

我想选择特定用户的(标记)(在 facebook 上的时间)(在 google 和 youtube 上的时间计数)

以下选择查询给出用户 ID '01'的(标记)(在 facebook 上的时间)

如何在同一查询中接收 id'1' 的 google 和 youtube 的时间计数?

SELECT table2.id,table2.marks, table1.time
FROM table1
RIGHT JOIN table2 ON table1.id= table2.id
WHERE table1.website LIKE ('%facebook%')
AND table1.id=  '01'
GROUP BY table1.id, table1.website

【问题讨论】:

  • 你希望你的输出是什么样的?显示它。

标签: mysql sql join count select-query


【解决方案1】:

您想在facebook 上找到时间,然后为特定用户找到youtubegoogle 的总和,您可以使用mysql conditional sum 来实现它

select
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
  sum(case when t1.website='google' then t1.time else 0 end)+
  sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
where t1.id = '01' 

如果您需要为所有用户计算相同的值,那么您可以这样做

select
t1.id,
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
  sum(case when t1.website='google' then t1.time else 0 end)+
  sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
group by t1.id

【讨论】:

  • 这是正确的,但它给出了两行。我怎样才能在一行中得到结果。请参考此链接s14.postimg.org/5x5evogr5/Capture.jpg
  • 标记表中是否有同一用户的多行?
  • 这是您提供的示例数据sqlfiddle.com/#!2/d5d1b/7
  • 非常感谢您的代码。但此时它不适用于 'group by' 关键字。我该如何解决?谢谢
  • 请用一些数据创建一个小提琴,以便我看看。
【解决方案2】:

如果我正确理解您的查询,我认为您需要使用子查询。 以下子查询返回两个计数; time_on_facebook & time_on_google_and_youtube 所有用户

SELECT t1.id, t2.marks, 
COUNT(t1.time) as time_on_facebook, 
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google") 
AND t1_sq.id = t1.id
GROUP BY t1.id)  as time_on_google_and_youtube 
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook"
GROUP BY t1.id

要将其限制为用户 id = 01,请添加 WHERE 子句

SELECT t1.id, t2.marks, 
COUNT(t1.time) as time_on_facebook, 
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google") 
AND t1_sq.id = t1.id
GROUP BY t1.id)  as time_on_google_and_youtube 
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook" AND t1.id = 1
GROUP BY t1.id

您确定要 COUNT(time) 还是要 SUM(time)? 最后,考虑为两个表添加一个主键,并可能将“id”列重命名为“user_id”以清楚起见。

【讨论】:

    【解决方案3】:

    不清楚您希望输出的样子。我做了一个查询, 但没有尝试。试试看,让我知道它是否有效。

    select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks 
    from table1 as t1
    left join table2 as t2
    on t1.id = t2.id
    where t1.website = 'facebook'
    and t1.id = '01'
    group by t1.id, t1.website
    
    UNION
    
    select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks 
    from table1 as t1
    left join table2 as t2
    on t1.id = t2.id
    where t1.website IN ('youtube', 'google')
    and t1.id= '01'
    group by t1.id, t1.website
    

    【讨论】: