In the following query, I am selecting the one column from users and am expecting a join to take place on both follow_reconciliations user_id and followed_user_id column.
SELECT u.ig_user_id FROM users u
LEFT JOIN follow_reconciliations f ON (f.followed_user_id = u.user_id)
LEFT JOIN follow_reconciliations f2 ON (f2.user_id = u.user_id)
But, I need to know which query the ig_user_id comes from. How can I rename ig_user_id as an alias for each join? What is the best approach to this?
EDIT: Visually, this is how I want the results to return
+------------+---------------------+
| ig_user_id | followed_ig_user_id |
+------------+---------------------+
| 15325 | 5295 |
+------------+---------------------+
This query might be more in the direction of this. It returns the correct results as two rows but they are not renamed as two aliases.
SELECT u.ig_user_id AS ig_user_id FROM users u LEFT JOIN follow_reconciliations f ON (f.user_id = u.user_id)
UNION
SELECT u2.ig_user_id AS followed_ig_user_id FROM users u2 LEFT JOIN follow_reconciliations f2 ON (f2.followed_user_id = u2.user_id)
Second Edit: This does the trick.
SELECT u.ig_user_id AS ig_user_id, u2.ig_user_id AS followed_user_id
FROM follow_reconciliations f
LEFT JOIN users u ON (u.user_id = f.user_id)
LEFT JOIN users u2 ON (f.followed_user_id = u2.user_id)
If I understand correctly (do I?) you only need one join:
SELECT
ig_user_id,
f.followed_user_id as followed_ig_user_id
FROM users u
LEFT JOIN follow_reconciliations f USING (user_id)
This gives me the results I am looking for:
SELECT u.ig_user_id AS ig_user_id, u2.ig_user_id AS followed_user_id
FROM follow_reconciliations f
LEFT JOIN users u ON (u.user_id = f.user_id)
LEFT JOIN users u2 ON (f.followed_user_id = u2.user_id)
If anyone has any performance suggestions, please chime in.
if i understand your question.. i'm sory if wrong..
SELECT u.ig_user_id,xx.followed_user_id FROM users u
LEFT JOIN (SELECT * FROM follow_reconciliations f) as xx ON (xx.followed_user_id = u.user_id)
LEFT JOIN (SELECT * FROM follow_reconciliations f2) as yy ON (yy.user_id = u.user_id)
this query provide you to rename or aliasing every column on table.. :)
CMIIW
Related
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.
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;
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.
I'm trying to left join the same table twice but it's giving me some problems. I have two tables ee_all and ee_calendar_events that I want to join.
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
SUM(e2.absent_hours)
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
LEFT JOIN ee_calendar_events AS e2 ON u.user_id = e2.sched_user_id AND e2.event_id = 2
WHERE
u.user_id = 23
The vacation_hours_earned column is supposed to return 133, which it does if I take out the second join. But soon as I add it, the query takes forever and the vacation_hours_earned has a value of 2000 or something (which is wrong). My guess is it's summing the row again when I add the second join, but I don't wan't that. I've been trying for a few hours but can't find a way around it, would appreciate any help.
When the rightmost table (the second join) has more than one row corresponding to a row of the table expression to the left, rows of the left-hand table expression are duplicated and count more than once in the SUM. Use subqueries instead.
SELECT
u.first_name,
u.last_name,
u.email,
(
SELECT
SUM(e1.total_vacation_hours_earned)
FROM
ee_calendar_events AS e1
WHERE
u.user_id = e1.sched_user_id
) AS vacation_hours_earned,
(similar) AS absent_hours
FROM
ee_all AS u
WHERE
u.user_id = 23
Using some MySQL syntax, you can just eliminate the second left join and simplify the query;
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
SUM(e1.absent_hours * (event_id=2))
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
WHERE
u.user_id = 23
Demo here.
SELECT
u.first_name,
u.last_name,
u.email,
SUM(e1.total_vacation_hours_earned) AS vacation_hours_earned,
(select SUM(e2.absent_hours) as absenthours from ee_calendar_events AS e2 where u.user_id = e2.sched_user_id AND e2.event_id = 2)
FROM ee_all AS u
LEFT JOIN ee_calendar_events AS e1 ON u.user_id = e1.sched_user_id
WHERE
u.user_id = 23
I'm struggling with this for hours so please help me.
This is my users table
id | username | last_activity(timestamp for online system)
And this is my friends table
id | uid | fid
What I want is to order the output by last_activity
My current query looks like this:
SELECT fid FROM friends WHERE uid='$user_id'
SELECT f.fid FROM f.friends
LEFT JOIN users u ON f.uid = u.id
WHERE uid=$user_id
ORDER BY u.last_activity DESC
You want to use INNER JOIN to join the two tables:
SELECT f.fid
FROM friends f
INNER JOIN users u
ON u.id = f.id
ORDER BY u.last_activity DESC
Always make sure you type out a real JOIN clause as some ways are old and getting more and more unsupported.
Read more here:
INNER JOIN on w3schools
I think you want
SELECT f.fid AS fid
FROM friends f, users u
WHERE f.uid = u.id AND f.uid = $user_id
ORDER BY u.last_activity DESC
I assume your problem is that fid is on one table, but the ordering criterion last_activity is on another table. The goal of this query is to JOIN each row in the friends table with the corresponding row in the users table (via the WHERE clause).
Assuming uid in friends table is foreign key references to id in users
table. so using INNER JOIN you can retrieve your desired results.
SELECT f.fid
FROM friends f INNER JOIN user_tb u
ON u.id = f.uid
WHERE f.uid = '$user_id'
ORDER BY u.last_activity DESC;