0

This is my table structure. sometime table1 data my repeat (Ex : Actually Id 1 should have only 4 rows but sometime it is 8 due to duplication) so avoid duplication I use GROUP BY command in select query

Table 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|

Table 2

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

I want to select (marks),(time on facebook) and (count of time on google & youtube) of specific user

Following select query gives (marks),(time on facebook) of user id '01'

How to receive count of time of both google and youtube of id'1' in same query ?

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
4

3 回答 3

1

你想找到时间facebook,然后求和,youtube对于google特定的用户,你可以使用mysqlconditional 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
于 2014-12-14T06:41:09.170 回答
0

如果我正确理解您的查询,我认为您将需要使用子查询。以下子查询返回两个计数;所有用户的 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”。

于 2014-12-14T06:27:34.217 回答
0

目前尚不清楚您希望输出看起来像什么。我进行了查询,但没有尝试。试试看,让我知道它是否有效。

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
于 2014-12-14T06:27:44.300 回答