How To write a SQL Select Query for this problem? - mysql

I have four tables: groups, users, votes, follows.
The structures of these tables are
groups
g_id g_title g_content
1 t1 content1
2 t2 content2
users
u_id u_groupid
1 1
2 1
3 2
4 2
5 2
votes
v_id v_userid v_groupid v_votes
1 1 1 1
2 1 2 1
3 2 2 1
4 3 2 1
5 3 1 1
follows
f_id f_userid f_groupid
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
The groups table records the basic information of a "group".
The users table keeps the relationship between users and groups, that is, if the user A belongs to groups B, then there will be a record in the user table.
The votes table means the supportive attitude that a user holds to a group.
If user A is interested in group A, then add a entry into the Follows table.
Now my problem is how to write one select statement to query the number of users, the number of votes and the number of followers of each group.
I want the query result likes this
g_id num_users unm_votes num_followers
1 2 2 3
2 3 3 2
By the way, my database is Mysql 5.0.51b.

If you want in 1 query, something like this will help you
SELECT g_id,
d1.num_users,
d2.unm_votes,
d3.num_followers
FROM groups gg
LEFT JOIN (SELECT g_id,
COUNT(u.u_id) AS num_users
FROM groups g
LEFT JOIN users u
ON u.u_groupid = g.g_id
GROUP BY g_id) d1
ON d1.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(v.v_userid) AS unm_votes
FROM groups g
LEFT JOIN votes v
ON v.v_groupid = g.g_id
GROUP BY g_id) d2
ON d2.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(f.f_userid) AS num_followers
FROM groups g
LEFT JOIN follows f
ON f.f_groupid = g.g_id
GROUP BY g_id) d3
ON d3.g_id = gg.g_id
GROUP BY gg.g_id

For the user count by group:
select g_id,count(u.uid) user_count from groups g, users u
where u.groupid = g.g_id
group by g_id
May want to read up on group by.

Related

Select all Groups and check if User is in a Group or not

I have two tables groups and groups_members
groups
id
name
1
GROUP 1
2
GROUP 2
3
GROUP 2
groups_members
group_id
user_id
1
123
2
123
2
555
1
4643
3
45434
Now I want to display ALL groups and check if the user i show the groups for (lets say user_id = 555) is in a group or not. Something like this:
SELECT g.id, g.name, is_in_group
FROM g.groups
JOIN groups_members gm ON gm.user_id = 555
Expected output:
id
name
is_in_group
1
GROUP 1
NULL
2
GROUP 2
1
3
GROUP 3
NULL
You have several ways to do so, a simple trick that comes to my mind is this one:
SELECT
g.*,
CAST(gm.user_id / gm.user_id AS UNSIGNED) AS is_in_group
FROM sgroups AS g
LEFT JOIN sgroups_members AS gm
ON g.id = gm.group_id
AND gm.user_id = 555;
-- id name is_in_group
-- 1 GROUP 1 NULL
-- 2 GROUP 2 1
-- 3 GROUP 3 NULL

SQL query two tables with a condition in the child table and record not exist in child

i have two tables (mysql):
channels:
uid
time
1
23423
2
52422
3
23423
4
42342
NULL
345
users:
uid
id
gid
1
sam1
1
2
sam2
2
3
sam2
2
4
sam2
3
i want to select all channel for users with gid=1 and gid=2 and for not existing users
i run query:
SELECT u.id, u.gid, c.time
FROM channels c
LEFT JOIN users u ON (u.uid=c.uid)
WHERE (u.gid IN (NULL,'1', '2'))
and have
uid
gid
time
1
1
23423
2
2
52422
3
2
23423
how to select a channel for a non-existent user ?
i want to get the following result:
uid
gid
time
1
1
23423
2
2
52422
3
2
23423
NULL
NULL
345
Use IS NULL to check for null values:
SELECT u.id, u.gid, c.time
FROM channels c
LEFT JOIN users u ON u.uid = c.uid
WHERE u.gid IN (1, 2) OR c.uid IS NULL;

get count(*) from multiple tables sql

I have three tables.
entry
ID title
1 Entry1
2 Entry2
3 Entry3
4 Entry4
user_likes
ID user_id entry_id
1 1 3
2 3 1
3 9 4
4 2 2
user_bookmarks
ID user_id entry_id
1 6 3
2 4 3
3 2 1
4 2 2
What i want is the sum of likes and bookmarks for each entry.
result
entryID likes bookmarks
1 1 1
2 1 1
3 1 2
4 1 0
Also with total sum of likes and bookmarks of each entry.
result2
entryID likes+bookmarks
1 2
2 2
3 3
4 1
I managed to get likes and bookmark result using this query in seperate tables. I was not able to show them together in a single table.
SELECT entry.id, COUNT(entry.id) AS likes FROM entry
INNER JOIN user_like ON user_like.entry_id = entry.id GROUP BY entry.id ORDER BY likes DESC
You should aggregate before joining:
select e.*, coalesce(l.likes, 0) as likes,
coalesce(b.bookmarks, 0) as bookmarks,
(coalesce(l.likes, 0) + coalesce(b.bookmarks, 0)) as both
from entries e left join
(select entryid, count(*) as likes
from likes l
group by entryid
) l
on l.entryid = e.id left join
(select entryid, count(*) as bookmarks
from bookmarks
group by entryid
) b
on b.entryid = e.id;

Filter count table query

i have 2 tables as following.
User
id name
---------------
1 john
2 raju
3 manu
4 raghu
friendtable
id userid recvId
------------------------
1 1 2
2 1 3
3 2 3
4 3 4
Is it possible to filter users by their friends count from these tables.Please help me.
For eg :- range >=3 will result : john,manu
range >3 and range <2 will result : raju
range <2 result : raghu
Do a UNION ALL to get all id's from friendstable in one column. Join users table with that result.
Do a GROUP BY, adjust HAVING to decide what to return, e.g. at least 3 times etc.
select u.name
from users
join (select userid as id from friendtable
union all
select recvId as id from friendtable) f
on u.id = f.id
group by u.name
having count(*) >= 3
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recvid>=3
SELECT name FROM user a,friendtable b WHERE a.id=b.id and b.recvid>3 AND b.recid<2
SELECT name FROM user a,friendtable b WHERE a.id=b.id AND b.recid<2

Mysql Group by counting issue

Database structure
Table 'applicants'
id org_id team_id
1 1 1
Table 'teams'
id name
1 Test
Table 'teams_members'
id team_id user_id
1 1 1
2 1 2
Table 'users_playeraccounts'
id user_id summoner_id rank_solo
1 1 1 5
2 1 2 8
3 2 3 7
select sum(rank_solo) as rank_sum,
max(rank_solo) as highest_rank,
count(tt.id) as members,
t.name,
o.team_id
from applicants o
join teams t on o.team_id = t.id
join teams_members tt on t.id = tt.team_id
join users_playeraccounts p on tt.user_id = p.user_id
where org_id = :org
group by team_id
This offcourse gives me a result like
rank_sum highest_rank members name team_id
20 8 3 Test 1
Is there a way for me to get both the count of members with their playeraccounts aka
If 1 user has 2 it'll be 2
And also a way for me to keep it as 1 so it literally just counts the rows found in teams_members neglecting the entries in users_playeraccounts?
I want to receive both 2 and 3 as a result of my query.
You want to count the distinct number of entries in tt.id, so you can do that like this:
SELECT ... COUNT(DISTINCT tt.id) AS distinct_members ...
Rather than giving you a count of every row that has a non-null tt.id, you'll get a count of the number of unique values.