I have a two tables: users and missionInfo. I need to select classes only when all of the users who are in that class completed all missions.
users table:
userID classID
1 1
2 1
3 1
4 2
5 2
missionInfo table:
userID missionID
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
5 1
5 2
So, I should get back classID 1 because users 1 thru 3 all completed all missions 1 thru 3 and are all part of the same class.
Help?
SELECT classID FROM (
SELECT classID
FROM users JOIN missionInfo USING (userID)
GROUP BY classID, missionID
HAVING COUNT(DISTINCT userID) = (
SELECT COUNT(*) FROM users u WHERE u.classID = users.classID
)
) AS t
GROUP BY classID
HAVING COUNT(*) = #total_number_of_missions
See it on sqlfiddle.
The DISTINCT can be omitted if (userID,missionID) will only appear once in the missionInfo table.
The inner query returns classID for every mission that has been completed by all that class's users; the outer one restricts the results to only those classes who have completed the specified number of missions.
Related
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;
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;
I want count for chat_question_id from chat_questions table based on user_id=1 in chat table.
Below is my chat_questions table
chat_question_id user_id
1 1
2 1
and the chat table
chat_id chat_question_id user_id
1 1 1
2 1 1
3 2 1
below is my query:
SELECT COUNT(cq.chat_question_id)
FROM chat_questions AS cq
LEFT JOIN chat AS c ONc.chat_question_id = cq.chat_question_id
WHERE c.user_id =2
I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)
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.