I want to get information from two tables users and results.
users table has
id, name, email, password columns etc.
and results table has
id, user_id, last_attempt_time etc.
The result table gets populated every time a user takes a quiz.
I want to display user_id,name and last_attempt_time but my query returns oldest time and i have no idea how to solve this problem.
SELECT u.id,u.email,u.name,u.joined,r.last_attempt_time FROM users u
LEFT JOIN results r
ON u.id=r.user_id
GROUP BY u.id
ORDER BY u.id ASC
Try:
SELECT u.id,u.email,u.name,u.joined,MAX(r.last_attempt_time) AS LastAttempt
FROM users u left join results r on u.id=r.quiz_id
GROUP BY u.id,u.email,u.name,u.joined
ORDER BY u.id ASC
Related
I want to get customer data from all the businesses with more than 1 user.
For this I think I need a subquery to count more than 1 user and then the outer query to give me their emails.
I have tried subqueries in the WHERE and HAVING clause
SELECT u.mail
FROM users u
WHERE count IN (
SELECT count (u.id_business)
FROM businesses b
INNER JOIN users u ON b.id = u.id_business
GROUP BY b.id, u.id_business
HAVING COUNT (u.id_business) >= 2
)
I believe that you do not need a subquery, everything can be achieved in a joined aggregate query with a HAVING clause, like :
SELECT u.mail
FROM users u
INNER JOIN businesses b on b.id = u.id_business
GROUP BY u.id, u.email
HAVING COUNT (*) >= 2
NB : in case several users may have the same email, I have added the primary key of users to the GROUP BY clause (I assumed that the pk is called id) : you may remove this if email is a unique field in users.
I want to combine two tables, so I have the user data and the actual data with statistics from the users. I combined both tables so I get one output.
SELECT users.username, data.followers, data.following, data.pins, users.url,
users.created, data.avatar, data.bio, data.date
FROM users
INNER JOIN data
ON data.userid=users.ID
ORDER BY data.followers DESC
Within that data I want only the latest record to be shown per user, based on the latest date. Hope someone can help me with this. Thanks in advance!
You just join to a query that has been grouped by to select max date by user.
SELECT users.username, data.followers, data.following, data.pins, users.url,
users.created, data.avatar, data.bio, data.date
FROM users
JOIN (select userid, max(date) as maxdate
from data
group by userid
) as md ON md.userid = users.id
JOIN data ON md.userid = data.userid and md.maxdate = data.date
ORDER BY data.followers DESC
I had a few typos that I fixed... here it is working http://sqlfiddle.com/#!9/2e7ad/2
I want to output users and their total number of wins and losses over requested date interval. When I run the query below within a date range that contains records in results table, everything works fine. However if a user does not have any records in results table for the requested date interval, then no user returned in the request at all. What I want is to return a user record anyway, even if the user does not have any records in results table for the requested date interval.
I believe the GROUP BY makes it behave that way, but I'm not sure how to configure the query to work the way I need it to.
SELECT
users.name,
users.division,
SUM(results.wins) as wins,
SUM(results.losses) as losses
FROM users LEFT JOIN results ON users.user_id = results.user_id
AND results.date BETWEEN {$startDate} AND {$endDate}
WHERE users.user_id = {$user_id} GROUP BY results.user_id;
The user is returned, just on a row where the id is NULL. You are grouping by the id in second table.
Instead, group by the first table field:
SELECT u.name, u.division,
SUM(r.wins) as wins, SUM(r.losses) as losses
FROM users u LEFT JOIN
results r
ON u.user_id = r.user_id AND r.date BETWEEN {$startDate} AND {$endDate}
WHERE u.user_id = {$user_id}
GROUP BY u.user_id;
---------^
I have a table for referred users (contains an email address and date columns) and a table for users.
I run to get the top referers:
SELECT count(r.Email) as count, r.Email
FROM refs r
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
But I want to join this with the users table so it displays with other data in the user table, I thought a join would work. Left join becuase emails may be entered incorrectly, some people put first name etc under refs.Email
SELECT count(r.Email) as count, r.Email, u.*
FROM refs r LEFT JOIN users u ON u.email_primary = r.Email
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
With the above query the count is incorrect, but I don't know why.
Try this one:
SELECT count(r.Email) as count, r.Email
FROM refs r
INNER JOIN users u ON u.email_primary = r.Email
WHERE r.referredOn > '2011-12-13'
GROUP BY email
ORDER BY count DESC
if your adding new column from users u you also need to add it on your group by clause.
Regards
Unfortunately, a LEFT JOIN wont help you here; what this type of join says is give me all the rows in users that match my email, as well as all the rows that have no match on email. If the email doesn't match, then they wont come back as you want.
So you can't use a the left join condition here the way you want.
If you enforced the fact that they had to enter an email everytime, and it was a valid email etc, then you could use an INNER JOIN.
JOINs are usually used to follow referential integrity. So, for example, I have a user with an id in one table, and another table with the column userid - there is a strong relationship between the two tables I can join on.
Jeft Atwood has a good explantion of how joins work.
SEE if this will help you:
SELECT e.count, e.email, u.col1, u.col2 -- etc
FROM (
SELECT count(r.Email) as count, r.Email
FROM refs r
WHERE r.referredOn > '2011-12-13'
GROUP BY email
) e
INNER JOIN
users u ON u.email_primary = e.Email
Instead of a direct join, you could TRY to use your counting query as a subquery-table type..
I wrote this query
SELECT *, count(r.Email) as count FROM refs r
LEFT OUTER JOIN users u ON r.email = u.email_primary
WHERE u.uid IS NOT NULL
GROUP BY u.uid
ORDER BY count DESC
Which showed me that the reason the count was wrong was because some of the email addresses are used twice in the users table (users sharing 'family' email address), this doubled my count, the above query shows each separate user account.
I am trying to figure out a complex (at least for me!) mysql query and hoped someone here might have some clue's for me..
I have 2 tables "Users" and "files".
Users:
id, name, address, etc..
Files:
id, user_id, file_name, etc..
I want to select all rows from Users and in the result create a last column that has a count of all file with where User.id = Files.user_id.
I tried SELECT * from Users UNION(SELECT COUNT Files.user_id WHERE Users.id = Files.user_id) but doesn't work of course..
select u.*, count(f.id)
from users u
left join files f on u.id = f.user_id
group by u.id