I have the two tables:
user_groups
id name created_user
1 gn1 3
2 gn2 3
user_group_has_users
group_id user_id
2 5
2 6
I want to count the users per group and an output similar this:
id name user_count
2 gn2 2
1 gn1 0
I tried this query
select g.*,
count(cg.user_id) as user_count
from user_groups as g
left join user_group_has_users as cg on cg.user_group_id = g.id
where g.created_user = 3
But it only returns one row. I do not understand why this happening.
Please help me on this.
Use this query
SELECT g.* ,COUNT(cg.user_id) AS user_count
FROM user_groups g
LEFT JOIN user_group_has_users cg ON cg.group_id = g.id
WHERE g.created_user = 3
GROUP BY g.id
ORDER BY user_count DESC ;
Left join with where condition is nothing but inner join. You may want as
select
ug.id,
ug.name,
count(ughu.user_id) as user_count
from user_groups ug
left join user_groups_has_users ughu on ughu.group_id = ug.id
and ug.created_user = 3
group by ug.id
order by user_count desc ;
If you still want to filter the data with the created_user = 3 then use inner join and move the condition to where clause
Be careful with aggregates and JOINs. In some cases the numbers are inflated.
See if this works for you:
select g.*,
( SELECT count(*)
FROM user_group_has_users
WHERE user_group_id = g.id
) AS user_count
from user_groups as g
where g.created_user = 3
Related
I have two tables
**users**
id name
1 Name1
2 Name2
**tasks**
id user_id title
1 1 Task1
2 1 Task2
3 2 Task3
My request:
SELECT
U.*,
COUNT(T.id) AS tasks_total
FROM
`#__users` AS U
LEFT JOIN
`#__tasks` AS T
ON
U.id = T.user_id
I think something wrong here...
I wan't to get results:
id name tasks_total
1 Name1 2
2 Name2 1
Thanks!
You forgot to group the result by id
SELECT
U.*,
COUNT(T.id) AS tasks_total
FROM
`#__users` AS U
LEFT JOIN
`#__tasks` AS T
ON
U.id = T.user_id
group by U.id
demo on sqlfiddle
You're mixing line functions and aggregate functions. One way around this is to have the aggregation done in a subquery:
SELECT
U.*,
tasks_total
FROM
`#__users` AS U
LEFT JOIN
(SELECT user_id, COUNT(*) AS tasks_total
FROM `#__tasks`
GROUP BY user_id) AS T
ON
U.id = T.user_id
I have a query that LEFT JOINS another table to group rows. I am trying to only select records from the first table "googleimage" where the user_id is a certain number. (user_id is a column in "googleimage" table.
SELECT g.*
FROM googleimage g
LEFT JOIN (SELECT image_id, COUNT(*) AS cnt
FROM googleimagefound WHERE status = 0
GROUP BY image_id) gf ON gf.image_id = g.id
ORDER BY COALESCE(cnt, 0) DESC");
I have tried adding a new ON statement beiside the
gf ON gf.image_id = g.id
I have also tried changing
SELECT g.*
FROM googleimage g
to
SELECT g.*
FROM googleimage WHERE user_id = 1 g
but none of them seem to work, any help will be appriciated
Try changing you query a bit like below
SELECT g.*
FROM googleimage g
LEFT JOIN (SELECT image_id, COUNT(*) AS cnt
FROM googleimagefound WHERE status = 0
GROUP BY image_id) gf ON gf.image_id = g.id
WHERE g.user_id = 1 <-- add this condition
ORDER BY COALESCE(gf.cnt, 0) DESC;
I'm wondering about these queries and I got a work that want to list all users in table user and count for their post photo and video. and can choose to view in sort by these count limit by ASC or DESC.
I've tried them both but see that sub-query is fast than join. i want to know the different between these queries. Why sometimes join is slower that a sub-query. is join best for only two tables? Is this both best for my work? or you can suggest another better solution.
SUB-QUERY
select
user.*,
(select count(*) from post where post.userid = user.id) postCount,
(select count(*) from photo where photo.userid = user.id) photoCount,
(select count(*) from video where video.userid = user.id) videoCount
from user order by postCOunt desc limit $starrow 20
JOIN
SELECT u.id,
COUNT(DISTINCT p.id) AS postCount,
COUNT(DISTINCT ph.id) AS photoCount,
COUNT(DISTINCT v.id) AS videoCount
FROM user u
LEFT JOIN post p
ON p.userid = u.id
LEFT JOIN photo ph
ON ph.userid = u.id
LEFT JOIN video v
ON v.userid = u.id
GROUP BY u.id
ORDER BY postCount LIMIT $startrow 20
Example in HTML page that order by postCount DESC and have paging.
userid postCount photoCount videCount
1 34 5 4
2 30 12 2
3 21 5 6
4 15 8 4
5 12 15 9
6 8 3 10
.. .. .. ..
You can try it this way with JOIN
SELECT u.id, postCount, photoCount, videoCount
FROM user u LEFT JOIN
(
SELECT userid, COUNT(*) postCount
FROM post
GROUP BY userid
) p ON p.userid = u.id LEFT JOIN
(
SELECT userid, COUNT(*) photoCount
FROM photo
GROUP BY userid
) ph ON ph.userid = u.id LEFT JOIN
(
SELECT userid, COUNT(*) videoCount
FROM video
GROUP BY userid
) v ON v.userid = u.id
ORDER BY postCount
LIMIT $startrow, 20
say i had the following tables
user_table
id username
1 abc
2 def
3 ghij
courses_table
id title
1 csc
2 math
3 syn
user_courses
user_id course_id
2 1
1 3
2 3
i want to select the username whos taking course 1 AND 3 ,
not at least 1 or 3 , i mean both 1 and 3
i've tried the following mysql queries
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id = 1 AND uc.course_id=3;
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN (1,3);
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN (1,3) AND uc.user_id = u.id ;
the first and third queries executed with no results shown , and the second one show all users who had at least course_id 1 or 3
if you are wondering why am i using the LEFT JOIN , this is because i need to join table's results , and the above line of code is just an example , and im using to get data from about 9 tables using the LEFT join .
any help please ? thanks
SELECT DISTINCT u.* FROM user_table as u LEFT JOIN user_courses as uc ON uc.user_id = u.id WHERE uc.course_id IN( 1,3) AND uc.user_id = 2 ";
this show me the result i want , its output "def" ,
but i can't use the user_id as a static value ( number 2 in this example )
This problem is called Relational Division
SELECT a.id, a.username
FROM user_table a
INNER JOIN user_courses b
ON a.id = b.user_ID
WHERE b.course_ID IN (1,3)
GROUP BY a.id, a.username
HAVING COUNT(*) = 2
SQL of Relational Division
If course_ID is not unique for every users considering that the user have retake the course, a DISTINCT keyword is needed to coung unique courses,
SELECT a.id, a.username
FROM user_table a
INNER JOIN user_courses b
ON a.id = b.user_ID
WHERE b.course_ID IN (1,3)
GROUP BY a.id, a.username
HAVING COUNT(DISTINCT b.course_ID) = 2
SQLFiddle Demo
OUTPUT
╔════╦══════════╗
║ ID ║ USERNAME ║
╠════╬══════════╣
║ 2 ║ def ║
╚════╩══════════╝
please try this:
SELECT
U.id,
U.username
FROM
user_courses UC
INNER JOIN user_table U
ON UC.`user_id` = U.`id`
WHERE UC.`course_id` = 1
OR UC.`course_id` = 3
GROUP BY U.`id`
HAVING COUNT(*) > 1
I have two tables in mysql database
groups
id|name
_______
1 |red
2 |blue
3 |green
4 |white
and users
id|name |group
_______________
1 |joe |1
2 |max |1
3 |anna |2
4 |lisa |2
So... joe and max are in the "red" group, anna and lisa are in the "blue" group.
How can I make simple listing of groups which would contain the number of
persons in that group
For example
red - 2
blue - 2
green - 0
white - 0
Most the other answers are basically correct, but forgot an important detail: GROUP is a reserved word in SQL, so your column name must be escaped:
SELECT groups.name, COUNT(*) AS total_members
FROM groups
LEFT OUTER JOIN users
ON users.`group` = groups.id
GROUP BY groups.id
check if this works....
SELECT COUNT(*), groups.name FROM groups, users WHERE users.group=groups.id GROUP BY groups.name
UPDATE
SELECT groups.name, COUNT(users.*) FROM groups LEFT JOIN users
ON groups.id=users.group GROUP BY groups.name
this will keep the colors even if they dont have any name related
Give this a try:
select g.name, count(u.id) from groups g
left join users u on g.id = u.group
group by g.id, g.name
Try this
SELECT COUNT(g.id) as count, g.name
FROM groups as g
LEFT JOIN users as u
ON u.group = g.id
GROUP BY g.id
This should work
SELECT g.*, COUNT(DISTINCT u.id) FROM `groups` g
INNER JOIN `users` u on g.id = u.group
GROUP BY u.id
maybe this should work
SELECT g.name, COUNT( u.id ) AS Totoal
FROM `groups` g
INNER JOIN `users` u ON g.id = u.group
GROUP BY g.id