Show latest dates from SQL table with INNER JOIN - mysql

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

Related

MYSQL: retrieve all the users details with the details of the first sale for each user by date, 2 tables

I'm trying to retrieve all the users details with the details of the first sale for each user by date.
Mysql tables:
enter image description here
my code:
SELECT u.id,u.name,u.email,s.amount,s.date
FROM users u
INNER JOIN sales s ON u.id=s.user_id
ORDER BY u.id,s.date
what its return:
[{"id":"1","name":"aaa","email":"aaa#gmail.com","amount":"5600","date":"2019-11-11"},{"id":"1","name":"aaa","email":"aaa#gmail.com","amount":"3000","date":"2020-01-08"},{"id":"2","name":"bbb","email":"bbb#gmail.com","amount":"6000","date":"2019-12-15"},{"id":"2","name":"bbb","email":"bbb#gmail.com","amount":"1000","date":"2020-06-05"},{"id":"3","name":"ccc","email":"ccc#gmail.com","amount":"7500","date":"2019-09-02"},{"id":"4","name":"ddd","email":"ddd#gmail.com","amount":"5000","date":"2019-03-12"},{"id":"4","name":"ddd","email":"ddd#gmail.com","amount":"4000","date":"2020-04-21"}]
I want to get the earliest date row of each id,like that:
[{"id":"1","name":"aaa","email":"aaa#gmail.com","amount":"5600","date":"2019-11-11"},{"id":"2","name":"bbb","email":"bbb#gmail.com","amount":"6000","date":"2019-12-15"},{"id":"3","name":"ccc","email":"ccc#gmail.com","amount":"7500","date":"2019-09-02"},{"id":"4","name":"ddd","email":"ddd#gmail.com","amount":"5000","date":"2019-03-12"}]>
help someone?
You can use a query that finds the first sales ID for each user as an inline view (a query in the from clause) and join that inline view to the two tables in question. Like this:
select u.*,
s.amount,
s.date
from users u
join (
select user_id,
min(date) as min_date
from sales
group by user_id
) v
join sales s
on u.user_id = v.user_id
and s.date = v.min_date;

Use SELECT through three table

I tried to write a query, but unfortunately I didn't succeed.
I want to know how many packages delivered over a given period by a person.
So I want to know how many packages were delivered by John (user_id = 1) between 01-02-18 and 28-02-18. John drives another car (another plate_id) every day.
(orders_drivers.user_id, plates.plate_name, orders.delivery_date, orders.package_amount)
I have 3 table:
orders with plate_id delivery_date package_amount
plates with plate_id plate_name
orders_drivers with plate_id plate_date user_id
I tried some solutions but didn't get the expected result. Thanks!
Try using JOINS as shown below:
SELECT SUM(o.package_amount)
FROM orders o INNER JOIN orders_drivers od
ON o.plate_id=od.plate_id
WHERE od.user_id=<the_user_id>;
See MySQL Join Made Easy for insight.
You can also use a subquery:
SELECT SUM(o.package_amount)
FROM orders o
WHERE EXISTS (SELECT 1
FROM orders_drivers od
WHERE user_id=<user_id> AND o.plate_id=od.plate_id);
SELECT sum(orders.package_amount) AS amount
FROM orders
LEFT JOIN plates ON orders.plate_id = orders_drivers.plate_id
LEFT JOIN orders_driver ON orders.plate_id = orders_drivers.plate_id
WHERE orders.delivery_date > date1 AND orders.delivery_date < date2 AND orders_driver.user_id = userid
GROUP BY orders_drivers.user_id
But seriously, you need to ask questions that makes more sense.
sum is a function to add all values that has been grouped by GROUP BY.
LEFT JOIN connects all tables by id = id. Any other join can do this in this case, as all ids are unique (at least I hope).
WHERE, where you give the dates and user.
And GROUP BY userid, so if there are more records of the same id, they are returned as one (and summed by their pack amount.)
With the AS, your result is returned under the name 'amount',
If you want the total of packageamount by user in a period, you can use this query:
UPDATE: add a where clause on user_id, to retrieve John related data
SELECT od.user_id
, p.plate_name
, SUM(o.package_amount) AS TotalPackageAmount
FROM orders_drivers od
JOIN plates p
ON o.plate_id = od.plate_id
JOIN orders o
ON o.plate_id = od.plate_id
WHERE o.delivery_date BETWEEN convert(datetime,01/02/2018,103) AND convert(datetime,28/02/2018,103)
AND od.user_id = 1
GROUP BY od.user_id
, p.plate_name
It groups rows on user_id and plate_name, filter a period of delivery_date(s) and then calculate the sum of packageamount for the group

How to include all usernames after filtering

NOTE: the p_date is supposed to be in dd-mm-yy format but excel decided to be a jerk as displayed it as dd-mm.
Anyways, for my query, I wanted to retrieve the username and a counter for the number of times the user has published a message in a particular month.
This is the code I used:
SELECT users.username, count(*)
FROM publishdate JOIN users
ON publishdate.uid = users.uid
WHERE MONTH(STR_TO_DATE(p_date, '%Y-%m-%d')) = 11
GROUP BY users.username;
When I filter for November, I see that ruby has published twice in that month, but how do I include all users who have published 0 messages into the result?
Thank you all in advance.
You have to use users table on the left side of your query and do LEFT JOIN to publishdate table:
SELECT users.username, count(publishdate.uid)
FROM users
LEFT JOIN publishdate ON publishdate.uid = users.uid AND
MONTH(STR_TO_DATE(p_date, '%Y-%m-%d')) = 11
GROUP BY users.username;
Note: Predicate MONTH(STR_TO_DATE(p_date, '%Y-%m-%d')) = 11 has to appear in the ON clause of the query, otherwise LEFT JOIN will be equivalent to an INNER JOIN operation.

Result of one query, input for other query in mysql

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

How to join 3 tables and get sum from result of individual joins?

I have the following scenario:
Table: users : user_id, username ,...
Table: login: user_id, login_date, ...
Table: point: user_id, points, point_time
Joins will be on the basis of users.user_id with other tables.
Now, I want to get count of all the logins as well as sum of all the points earned by the user.
Now, when I do:
select users.user_id,count(*) from users
inner join login on users.user_id=login.user_id
group by users.user_id
It returns count as 36(for example).
Whenever I run:
select users.user_id,count(*),sum(points) from users
inner join point on users.user_id=point.user_id
group by users.user_id
It returns sum as 400(for example) and count as 2.
But if I combine both the queries:
select users.user_id,count(*),sum(points) from users
inner join login on users.user_id=login.user_id
inner join point on users.user_id=point.user_id
group by users.user_id
It returns count as 72 (36 * 2) and sum as 800 (400 *2).
Twice because of multiple userIds present.
I tried several things like combining with distincts but nothing seems to work. Please help.Better if it's possible with joins alone. Thanks in advance. I am using mysql with Php.
You can sum the points in a subquery and select distinct logins in the count
select users.user_id,l.login,p.points from users
inner join (select user_id, count(1) login from login
group by login) as l on users.user_id=login.user_id
inner join (select user_id, sum(point) as point
from point group by user_id ) as p on users.user_id=point.user_id
You should be able to do your count by joining in your login table and then including a subquery to get your count of points:
select users.user_id, count(*) as login_count,
(select sum(points) from point
where point.user_id = users.user_id) as points_sum
from users
inner join login on users.user_id=login.user_id
group by users.user_id