My Join statement isn't working - mysql

I need help with a sql statement.
table users:
user_id name
----------------------
1 joe
2 sam
5 tammy
6 jade
10 tony
table a1:
id user_id year approved
----------------------------------
1 5 2012 1
2 6 2012 0
3 6 2013 1
4 5 2013 1
5 10 2012 0
6 10 2013 0
I need to return all 5 user_id wither or not they have an entry in table a1 and I need fields: a1.id, users.user_id, users.name, a1.year, a1.approved from the join.
My attempt:
select a1.id, users.user_id, users.name, a1.year, a1.approved from users
LEFT JOIN a1 as a1 ON a1.user_id = users.user_id
Im stuck.
Thanks

Related

Select from two tables but only for the maximum values in one of the columns

I have the following two tables. I want to select all the users with user_type 1, as well as the details for their most recent note. I want to order the results by the note time_created.
users table:
user_id name user_type
1 Joe 1
2 Fred 1
3 Sam 0
4 Dave 0
notes table:
notes_id user_id note time_created
1 1 Joe 1 2019-06-05 13:45:00
2 1 Joe 2 2019-06-05 13:46:00
2 1 Joe 3 2019-06-05 13:47:00
3 2 Fred 1 2019-06-05 13:45:00
4 2 Fred 2 2019-06-05 13:46:00
5 3 Sam 1 2019-06-05 13:45:00
6 4 Dave 1 2019-06-05 13:45:00
So the result of the query would be
user_id name note time_created
1 Joe Joe 3 2019-06-05 13:47:00
2 Fred Fred 2 2019-06-05 13:45:00
This is my effort so far
SELECT users.user_id, users.name, notes.note, notes.time_created FROM notes
INNER JOIN users ON users.id=prospect_notes.subject_id
WHERE users.user_type = 1 ORDER BY notes_time_created;
It is pretty good, but it returns several rows per user. I just want the one row containing the maximum time created.
You can use a correlated subquery:
SELECT u.user_id, u.name, n.note, n.time_created
FROM users u JOIN
notes n
ON u.id = n.subject_id
WHERE u.user_type = 1 AND
n.notes_time_created = (SELECT MAX( n2.notes_time_created )
FROM notes n2
WHERE n2.subject_id = n.subject_id
)
ORDER BY n.notes_time_created;

JOIN MySQL tables based on condition

I have two MySQL tables users and task_reg
users
id name
1 user1
2 user2
3 user3
4 user4
5 user5
6 user6
7 user7
8 user8
9 user9
task_reg
top_id use_id payment
1 1 1
1 2 1
1 4 1
2 3 1
2 5 1
I have to display all the users and also show whether they have paid or not
here is my SQL command but it only shows the paid users
SELECT users.id, users.name, task_reg.payment
FROM task_reg
JOIN users ON users.id = task_reg.use_id
JOIN topics ON topics.id = task_reg.top_id
WHERE topics.id = 1
Result
id name payment
1 user1 1
2 user2 1
4 user4 1
Expected result
id name payment
1 user1 1
2 user2 1
3 user3 0
4 user4 1
5 user5 0
6 user6 0
7 user7 0
8 user8 0
9 user9 1
DB-Fiddle
Thanks.
Since you want all the users, it should be your first table in the join order.
Change your Inner join to Left Join.
Where condition on the topics.id should be a Join condition.
Use Coalesce() function to get value of payment done as 0 (because of no matching rows found).
Try:
SELECT users.id, users.name, COALESCE(task_reg.payment, 0) AS payment
FROM users
LEFT JOIN task_reg ON users.id = task_reg.use_id
LEFT JOIN topics ON topics.id = task_reg.top_id AND topics.id = 1

SQL Find All user that have two history in a certain order

I'm looking for a SQL request that I can't find in internet (and I didn't found a solution myself).
I have two different table user and history and a table user_history that link the two tables.
For example :
USER
id name
1 John
2 Edie
3 France
4 Gabriel
5 Ellen
History
id date_entered type
1 2017-07-01 36
2 2017-07-02 52
3 2017-07-03 25
4 2017-07-04 69
5 2017-07-05 85
6 2017-07-06 74
7 2017-07-07 45
8 2017-07-08 85
9 2017-07-09 25
10 2017-07-10 78
USER_HISTORY
id id_user id_history
1 1 1
2 1 2
3 1 3
4 1 4
5 2 5
6 2 6
7 1 7
8 1 8
9 2 9
10 1 10
In this example, all history are made by user 1 and 2 (user 2 have history 5,6 and 9).
So the question is :
What is the SQL request that get me all the users that have in their history an history type 25 and then some days LATER an history type 85 ?
In this example, only user 1 (John) is ok because he has a history type 25 on 2017-07-03 and then an history type 85 on 2017-07-08.
User 2 (Edie) is not ok because even if he has an history 25 and 85, the first one was 85 and the 25.
Is that clear ?
Can you help me please ?
You need to JOIN twice with HISTORY table, e.g.:
SELECT h1.id_user
FROM (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 25) h1
JOIN (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 85
) h2 ON h1.id_user = h2.id_user
WHERE h1.date_entered < h2.date_entered;
Here's the SQL Fiddle.

MySQL sum column and display last row in column

I have a table of users,subscription packages and various user subscriptions.
I need to fetch a sum of all subscription cost and display the latest/last subscription. The latest subscription is the subscription
with the highest subscription_id. How can I write my query? My tables are listed as below.
Users table
user_id name
1 John
2 Jane
3 Matthew
Subscription Packages table
package_id package_name
1 Basic
2 Advanced
3 Premium
User Subscriptions
subscription_id user_id package_id subscription_cost date
1 1 1 2 2014-04-01
2 2 1 2 2014-04-01
3 3 1 2 2014-04-01
4 1 1 2 2014-05-01
5 1 2 3.5 2014-06-01
6 2 2 3.5 2014-06-01
7 2 2 3.5 2014-07-01
8 1 3 5 2014-07-01
9 3 2 5 2014-07-01
10 2 2 3.5 2014-08-01
11 1 1 2 2014-08-01
My results should be like so
name total_costs latest_package
John 14.5 Basic
Jane 12.5 Advanced
Matthew 7 Premium
Because you need to do an aggregation anyway, I would go for the group_concat()/substring_index() trick:
select u.user_id, u.name, sum(subscription_cost) as total_costs,
substring_index(group_concat(p.package_name order by us.date desc), ',', 1) as latest_package
from usersubscriptions us join
users u
on us.user_id = u.user_id join
packages p
on us.package_id = p.package_id
group by u.user_id;
This assumes that no package names have commas. It is also subject to default limits on the length of the result of group_concat() -- but this method often works in practice.

Mysql SUM 0 when not in another table

I have 2 tables:
users:
id | name | club_id |
1 Bob 4
2 Jane 5
3 Alex 4
4 Paul 4
5 Tom 4
points:
user_id | club_id | amount(can vary)
1 4 10
1 2 10
2 5 10
3 4 10
3 4 10
4 4 10
3 2 10
3 4 10
I need (where users.club_id = 4 AND points.club_id = 4):
user_id | name | sum(amount)
3 Alex 30
1 Bob 10
4 Paul 10
5 Tom 0
Notice Tom is present in users but doesn't have any entries in points, so his sum should be 0. This is what throws me off in conjuction with grabbing a list from users.
Also would like this to be as efficient as possible (hence I added club_id = 4 both in users and points)
Try this:
SELECT
u.id,
u.name,
COALESCE(SUM(p.amount), 0) AS Totalpoints
FROM
(
SELECT *
FROM users
WHERE club_id = 4
) AS u
LEFT JOIN
(
SELECT *
FROM points
WHERE club_id = 4
) AS p ON p.user_id = u.id
GROUP BY u.id,
u.name;
See it in action here:
SQL Fiddle Demo
try this query
select u.id, u.name, sum(if (p.amount is null, 0, p.amount)) as totalPoint
from
user u
left join
(select * from point p where p.club_id = 4)p
on u.id = p.user_id
where u.club_id=4
group by u.id
SQL FIDDLE: