How to properly LEFT JOIN users with transactions - php

SELECT *
FROM users
LEFT JOIN payments ON payments.user_id = users.user_id
WHERE payments.id = (SELECT max(p2.id)
FROM payments p2
WHERE payments.user_id = p2.user_id)
Is the query I am currently using to select the user info, as well as their last transaction. The problem is that I'd like to select the users even if they do not have a transaction in the database, how do I go about this?
My table structure I'm playing with is very simple:
USERS - user_id, name
PAYMENTS - id, user_id, method

You might be better off (performance wise) joining onto a sub query like this:
SELECT *
FROM users u
LEFT JOIN
(Select user_id, max(id) from payments group by user_id) p
ON p.user_id = u.user_id
By putting any conditions in the Where clause, you are turning it into an inner join,

try something like this:
SELECT users.*, payments.*
FROM users
LEFT JOIN payments ON payments.user_id = users.user_id
AND payments.id = (SELECT max(p2.id)
FROM payments p2
WHERE payments.user_id = p2.user_id)
You have two columns "user_id" in the result set, one from users table, and one from payments. The payments.user_id will be null, but users.user_id should have value.

Why is everybody doing all that crazy stuff?
select u.user_id u_uid, u.name, p.user_id p_uid, p.method from users u left join payments p on (u.user_id = p.user_id);

Related

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

How to perform inner join in mysql

I have to write a query such that ,I need to get events whose start date is of 30 min from now.
My conditions are:
1) get the event from events table
2)Join created by of events with id in users table.
3)Comments from comment table with user ser id
But the problem here is if there is no comment for event then the event it self is not coming.If any comment is present it is coming.I dont want this.If comment is not there just fetch it as empty but not hide the total event .Can anyone please help me,.Thanks.
select u.email ,group_members.user_id,users.first_name,u.first_name
as host_name,events.name,events.start_date,comments.comments,c.first_name as
comment_user,comments.id from events
inner join users as u on u.id = events.created_by
inner join comments on events.id = comments.event_id
inner join group_members on events.group_id = group_members.group_id
inner join users as c on comments.from_user = c.id
inner join users on group_members.user_id = users.id
where events.start_date between date_add(now(),interval 1 minute) and date_add(
now(),interval 30 minute)
and group_members.user_status = 2
and events.status = 2
You need a left join to the comments table. I would put that table last in the from clause.
select u.email, gm.user_id, gu.first_name, u.first_name as host_name,
e.name, e.start_date, c.comments, uc.first_name as comment_user,
c.id
from events e inner join
users u
on u.id = e.created_by inner join
group_members gm
on e.events.group_id = gm.group_id inner join
users gu
on gm.user_id = gu.id left join
comments c
on e.id = c.event_id left join
users uc
on c.from_user = uc.id
where e.start_date between date_add(now(),interval 1 minute) and date_add(now(),interval 30 minute) and
gm.user_status = 2 and
e.status = 2;
Once you use a left join on comments, you also need a left join for the from user. I replaced all table names with aliases -- this makes it easier to track which table is used for which purpose.
Use the INNER JOIN Keyword and select the two columns by putting them with keyword ON.
SELECT EMP.EMP_ID, EMP.EMP_NAME, DEPT.DEPT_NAME FROM EMP
INNER JOIN DEPT ON DEPT.DEPT_ID = EMP.DEPT_ID;

SQL - join selected data or table for conditions

I have SQL tables for users and relationships, like
users{ID, name, email}
relationships{ID, uid, relid, type}
I want to get all "friends" ID, name and email (by current user ID) by SQL, using JOIN or whatever.
If using join is it better to JOIN the table of users and select relationships or select relationships and join users?
something like this (I invented now, i use a bit diferrent tables and code, sorry for errors)
1:
SELECT DISTINCT u.ID, u.name, u.email FROM users u JOIN relationships r ON (r.uid = 1 OR r.relid = 1) AND r.type = 1 WHERE u.ID = r.uid OR u.ID = r.relid
2:
SELECT DISTINCT u.ID, u.name, u.email FROM relationships r JOIN users u ON r.uid = u.ID OR r.relid = u.ID = 1 WHERE r.uid = u.ID OR r.related = u.ID
Edit:
I use DISTINCT becouse when I select users whoose IDs equals to r.uid or r.related, I also get the current user (uid + related) for every relation. DISTINCT should unique the users? I'm beginner and found this on stackoverflow.
So, is it better to select from users and join relations or select relations and join users?
BTW: I quite like the answer with UNION
You can union the users where user is in the uid and in the relid.
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.uid
WHERE users.ID = 500
UNION
SELECT users.*
FROM users
INNER JOIN relationships AS rel
ON users.ID = rel.relid
WHERE users.ID = 500
Not sure why you are using DISTINCT but it looks like you just want to select all the rows from the relationships table (the friends), and then join their details from the users table.
SELECT u.id, u.name, u.email
FROM relationships r
LEFT JOIN users u ON u.id = r.uid
WHERE r.uid = 12345

MySQL: Select rows where `project_id` equals any `project` with specific `user_id`

This can't be too hard, but I don't know what the term is I'm looking for. I'm sure you guys can help me out. :)
I have a table tasks with rows that have a column project_id. Each project_id refers to (the id of) a row in the projects table. Each project belongs to a certain user which is why it has a column user_id.
I now want to select all tasks from this table where the project_id equals any project of a certain user.
Or put more simply:
Each TASK belongs to a PROJECT which belongs to a USER.
I want to create a SELECT-statement to receive all TASKS that belong to a specific USER. The only link between each TASK and a USER is through the PROJECT.
How do I accomplish this?
Thank you very much for your help! :)
JOIN the tables:
SELECT
t.*
FROM tasks t
INNER JOIN projects p ON t.project_id = p.project_id
INNER JOIN users u ON p.user_id = u.user_id
WHERE u.user_id = #AcertianUserId
SELECT
tasks.*
FROM
users
INNER JOIN
projects ON users.id = projects.user_id
INNER JOIN
tasks ON projects.id = tasks.project_id
WHERE
users.id = 1
SQL Fiddle
select t.*
from tasks t
join projects p on p.id = t.project_id
join users u on u.id = p.user_id
where u.id = 17
Try :
SELECT t.*
FROM user u, projet p, task t
WHERE u.id = p.user_id
AND p.id = t.project_id
AND u.id = your_id
Or (same result) :
SELECT t.*
FROM user u
INNER JOIN projet p ON u.id = p.user_id
INNER JOIN task t ON p.id = t.project_id
WHERE u.id = your_id

PHP/MYSQL Query if not in second table

I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.

Categories