How to count and get result of two rows in Mysql Select Query?

258 Views Asked by At

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
3

There are 3 best solutions below

5
On BEST ANSWER

You want to find the time on facebook and then the sum of youtube and google for a particular user you can use the mysql conditional sum to achieve it

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' 

If you need to calculate the same for all the users then you can do it as

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
0
On

If I understand your query correctly, I think you will need to use a subquery. The following subquery returns two counts; time_on_facebook & time_on_google_and_youtube for all users

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

To restrict it to user id = 01, add in a WHERE clause

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

Are you sure you want COUNT(time) or do you want SUM(time)? Lastly, consider adding a primary key to both tables and maybe rename the "id" column to "user_id" for clarity.

0
On

Its not clear what you want the output to look like. I made a query, but did not try it. Try it and let me know if it works.

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