I'm searching my submission fields for users that signed up for two different projects. This is what I have, that is not working correctly. Any help would be great!
SELECT
user_id, COUNT(*)
FROM submissions
WHERE
project_id = 125
or project_id = 81
group by
user_id
HAVING COUNT(*) >= 2
So to clarify, I want to know what users have a submission from project_id 81 AND project_id 125. Each of the submissions
The right sintax is this one, you're missing a *
SELECT
user_id, COUNT(*)
FROM
submissions
WHERE
project_id = 125 or project_id = 81
GROUP BY
user_id
HAVING
COUNT(*) >= 2
in case a user can submit the same project multiple times, it's better to write your HAVING condition like this:
HAVING COUNT(DISTINCT project_id)>=2
so we can be sure that it will match two different distinct projects, and not just one project submitted multiple times
Related
I want to SELECT the total count of page likes I have in common with each other user in the database.
When I'm trying this statement ...
SELECT
l.item_id,
l.uid,
COUNT(l.item_id)
FROM
pages_likes l
WHERE
l.uid IN (NOT NULL, 544)
GROUP BY
l.uid
... it only outputs one single record of my own total count of page likes and my user id. In this example my user id is 544.
So I only get one record back instead of multiple records of users like I expected which should show different user id's (named uid) and different total counts of likes.
So my WHERE-condition is obviously the wrong one to go with and I would be grateful for an advise what other possibities I have to solve this issue.
Table page_likes structure:
id, item_id, uid, date
Try this:
SELECT
l1.uid,
COUNT(*) AS common_likes
FROM
page_likes AS l1
JOIN page_likes AS l2 ON l1.item_id = l2.item_id
WHERE
l1.uid != 544
AND l2.uid = 544
GROUP BY l1.uid
ORDER BY
common_likes DESC
This joins subsets of the page_likes table with itself. One subset is everything liked by other users, the other is your likes. Then it counts the number of items in the pair with each other user.
I have two tables which join themselves by a field called user_id. The first table called sessions can have multiple lines for the same day. I'm trying to find a way of selecting the total of that sessions without repeating the days (sort of).
Example:
Table sessions
ID | user_id | datestart
1 1 2014-08-05
2 1 2014-08-05
3 2 2014-08-05
As you can see there are two lines that are repeated (the first and second). If I query SELECT COUNT(sess.id) AS total this will retrieve 3, but I want it to retrieve 2 because the first two lines have the same user_id so it must count as one.
Using the clause Group By will retrieve two different lines: 2 and 1, which is also incorrect.
You can view a full example working at SQLFiddle.
Is there anyway of solving this only by query or do I need to do it by language?
I think you are looking for count(distinct):
SELECT COUNT(distinct user_id) AS total
FROM sessions sess INNER JOIN
users user
ON user.id = sess.user_id
WHERE user.equipment_id = 1 AND
sess.datestart = CURDATE();
If I understand the problem correctly, you want the number of users with sessions, rather than number of unique sessions. Use DISTINCT:
SELECT COUNT(DISTINCT(user_id)) FROM sessions,users WHERE user_id=users.id
Try this way:
SELECT COUNT(distinct sess.user_id) AS total
FROM sessions AS sess
INNER JOIN users AS user ON user.id = sess.user_id
WHERE user.equipment_id = 1 AND sess.datestart = CURDATE()
Sql Fiddle
I currently have 2 tables:
Favorite:
userID
drinkName
History:
userID
drinkName
I want to get the sum of the total times a specific userID shows up in each table, and then then the total number of times userID shows up in both tables.
(SELECT COUNT(userID) AS totalDrinks FROM History
WHERE userID = 'sai') union
(SELECT COUNT(userID) AS totalDrinks FROM Favorite
WHERE userID = 'sai')
So that code gets me the following output:
totalDrinks
4
2
However I am trying to use the MySQL sum function and that's not adding the two things up though.
So I was wondering how I would rewrite my query to output 6?
SELECT SUM(userID)as totalDrinks FROM History h
JOIN Favorite f ON h.userID=f.userID
GROUP BY userID
WHERE userID = 'sai'
Your UNION approach was almost there. You will have to SUM the result of both queries:
SELECT SUM(totalDrings) totalDrings FROM (
SELECT COUNT(*) totalDrinks FROM History
WHERE userID = 'sai'
UNION ALL
SELECT COUNT(*) FROM Favorite
WHERE userID = 'sai'
) s
A few things to note. You should use UNION ALL otherwise if the COUNTs result in the same number then they will be added only once. Another thing is that you should not use an INNER JOIN in here as that will force the users to be present in both tables.
A bit of background info; this is an application that allows users to created challenges and then vote on those challenges (bog standard userX-vs-userY type application).
The end goal here is to get a list of 5 users sorted by the number of challenges they have won, to create a type of leaderboard. A challenge is won by a user if it's status = expired and the user has > 50 votes for that challenge (challenges expire after 100 votes in total).
I'll simplify things a bit here, but essentially there are three tables:
users
id
username
...
challenges
id
issued_to
issued_by
status
challenges_votes
id
challenge_id
user_id
voted_for
So far I have an inner query which looks like:
SELECT `challenges`.`id`
FROM `challenges_votes`
LEFT JOIN `challenges` ON (`challenges`.`id` = `challenges_votes`.`challenge_id`)
WHERE `voted_for` = 1
WHERE `challenges`.`status` = 'expired'
GROUP BY `challenges`.`id`
HAVING COUNT(`challenges_votes`.`id`) > 50
Which in this example would return challenge IDs that have expired and where the user with ID 1 has > 50 votes for.
What I need to do is count the number of rows returned here, apply it to each user from the users table, order this by the number of rows returned and limit it to 5.
To this end I have the following query:
SELECT `users`.`id`, `users`.`username`, COUNT(*) AS challenges_won
FROM (
SELECT `challenges`.`id`
FROM `challenges_votes`
LEFT JOIN `challenges` ON (`challenges`.`id` = `challenges_votes`.`challenge_id`)
WHERE `voted_for` = 1
GROUP BY `challenges`.`id`
HAVING COUNT(`challenges_votes`.`id`) > 0
) AS challenges_won, `users`
GROUP BY `users`.`id`
ORDER BY challenges_won
LIMIT 5
Which is kinda getting there but of course the voted_for user ID here is always 1. Is this even the right way to go about this type of query? Can anyone shed any light on how I should be doing it?
Thanks!
I guess the following script will solve your problem:
-- get the number of chalenges won by each user and return top 5
SELECT usr.id, usr.username, COUNT(*) AS challenges_won
FROM users usr
JOIN (
SELECT vot.challenge_id, vot.voted_for
FROM challenges_votes vot
WHERE vot.challenge_id IN ( -- is this check really necessary?
SELECT cha.id -- if any user is voted 51 he wins, so
FROM challenges cha -- why wait another 49 votes that won't
WHERE cha.status = 'expired' -- change the result?
) --
GROUP BY vot.challenge_id
HAVING COUNT(*) > 50
) aux ON (aux.voted_for = usr.id)
GROUP BY usr.id, usr.username
ORDER BY achallenges_won DESC LIMIT 5;
Please allow me to propose a small consideration to the condition to close a challenge: if any user wins after 51 votes, why is it necessary to wait another 49 votes that will not change the result? If this constraint can be dropped, you won't have to check challenges table and this can improve the query performance -- but, it can worsen too, you can only tell after testing with your actual database.
I've got a mySQL database of survey responses. Each one has a userID, a questionID and the actual answer. Now I'm trying to write a report that will tell me how many people actually completed the survey as opposed to stopped halfway through. So I'm trying to figure out how to write a query that will count all of the userIDs that are duplicated exactly 91 times.
Be gentle, this is my first stackoverflow question.
You have to group by having count(*) = 91
select userId from myTable group by userId having count(*) = 91
http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html
I don't have data to test this, but this may help:
SELECT COUNT(*) AS userCount, userId
FROM tbl
GROUP BY userId
HAVING userCount = 91
If the questions are ordered, then you could check for the last question ID:
SELECT COUNT(*) FROM SurveyResponses WHERE QuestionId= [last_question_id]