LEFT OUTER JOIN with four table - mysql

Here is the table structure
User table
Column
fb_id
email
name
fname
lname
gender
fb_link
created
referral table
Column
id
referred_by
joinee
created
Currently implemented one SQL is (Example 1)
SELECT u.fb_id
,fb_link
,name
,r.referred_by
,u.created
FROM users u
LEFT OUTER JOIN referral r
ON u.fb_id=r.joinee
result of above query
joinee referer
10152250261037651 NULL
10152604594389921 NULL
10154430845000507 1518673071699780
....
....
....
....
547146735389782 NULL
Here is the sql (Example 2)
SELECT u_joinee.fb_id joinee_fb_id
,u_referer.fb_id referer_fb_id
,u_joinee.NAME joinee_name
,u_referer.NAME referer_name
FROM users u_joinee
,users u_referer
,referral r_j
,referral r_r
WHERE u_referer.fb_id = r_r.referred_by
AND u_joinee.fb_id = r_j.joinee
result of above query
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
What I am looking out for is I need both name in joinee_name and referer_name in the output. But I ma not able to use four tables in LEFT OUTER JOIN clause like i did for example 1 I want to fetch both records which include both non referral and referral joinees Is there any way to expect required result as shown below
Expected result
joinee_fb_id referer_fb_id joinee_name referer_name
10154430845000507 1518673071699780 Saselsdein Bsdasd
10154430845000347 Null asd NULL
10154430845000567 Null asd asdm NULL
10154230845000567 Null Dsd asdm NULL
10154330845000567 Null sdm NULL
101544553045000567 Null Esd aedm NULL

You can join with the users table twice by giving it different aliases, just like you do with your implicit inner join.
I don't think you need to join with referral twice. A single row in the referral table links a referrer and a joinee.
SELECT u_joinee.fb_id joinee_fb_id ,u_referer.fb_id referer_fb_id, u_joinee.name joinee_name, u_referer.name referer_name
FROM users AS u_joinee
LEFT JOIN referral AS r ON r.joinee = u_joinee.fb_id
LEFT JOIN users AS u_referer ON r.referred_by = u_referer.fb_id

Related

How to left join on mysql between two tables?

Hi i'm trying to left join from a vessel table to daily_volume table. The two table contains the following data:
(vessel_table)
(daily_volume table)
Let's say today is 2019-07-08. I would like to left join vessel_table to daily_volume table in a way it will show this output as brand new:
My current sql statement is this but doesn't seem to achieve the above output:
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
Can anyone help me how should i query to produce the output i need? Thanks
i have tried looking stackoverflow but to no avail
SELECT * FROM vessel LEFT JOIN daily_volume ON vessel.vessel_id = daily_volume.vessel_id WHERE daily_volume.date is null
vessel_id volume date
1 null null
2 null null
3 null null
4 null null
5 null null
6 null null
You seem to want a condition on the current date in the ON clause:
SELECT v.vessel_id, dv.volume, dv.date
FROM vessel v LEFT JOIN
daily_volume dv
ON dv.vessel_id = dv.vessel_id AND
dv.date = curdate();

RIGHT JOIN is returning all NULL results

I have two tables:
I'm trying to retrieve fields name, quantity and custom_message from table 1 and join the correct associated field value from table 2 - to give me:
name | quantity | custom_message | value
This is my query:
SELECT vxu_4_wpsc_cart_contents.name, vxu_4_wpsc_cart_contents.quantity,
vxu_4_wpsc_cart_contents.custom_message
FROM vxu_4_wpsc_cart_contents
RIGHT JOIN vxu_4_wpsc_submited_form_data
ON vxu_4_wpsc_cart_contents.id = vxu_4_wpsc_submited_form_data.id
WHERE form_id =2
OR form_id =3
which is returning
name | quantity | custom_message
NULL NULL NULL
NULL NULL NULL + 3 more rows of nulls
vxu_4_wpsc_cart_contents:
table 1
vxu_4_wpsc_submited_form_data:
I just don't know where I'm going wrong!
I believe right join should be inner join. Try the following query:
SELECT
vcc.name,
vcc.quantity,
vcc.custom_message,
vfd.value
FROM
vxu_4_wpsc_cart_contents AS vcc
INNER JOIN vxu_4_wpsc_submited_form_data as vfd
ON vcc.purchaseid = vfd.log_id
WHERE
vfd.form_id IN (2,3);
(fyi: your second image link is the same as the first :))

SQL Join for retrieving record for a single user

I have user related info split into multiple tables . I am trying to write a join to retrieve data for a single user , a lot of the info is optional , so the entry for many columns may be null , which is okay . I have written the following query , it is working except that it returns all users when I want the user with id '69'
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN (
SELECT *
FROM cur_website
WHERE Userid =69
) AS w ON cur_doctor_names.UserId = w.Userid
I want the following result :
First_Name | Last Name | Website
ABC XYZ Null
where ABC is the name for user with id 69 .
You are only filtering the websites because you are using a left join. You should apply your filter to the doctors table
SELECT
cur_doctor_names.First_Name,
cur_doctor_Names.Last_Name,
w.Website
FROM
cur_doctor_names
LEFT JOIN cur_website AS w
ON cur_doctor_names.UserId = w.Userid
WHERE
cur_doctor_names.UserId = 69
Try writing it like this - your where clause was in the wrong place (also your left join does not need to be written like that):
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website as w
ON cur_doctor_names.UserId = w.Userid
WHERE cur_doctor_names.userId = 69
I think you can just write
SELECT cur_doctor_names.First_Name, cur_doctor_Names.Last_Name, w.Website
FROM cur_doctor_names
LEFT JOIN cur_website w ON cur_doctor_names.UserId = w.Userid
WHERE Userid =69

Mapping table MySQL / Access

I have a short access/mySQL question. I have a mapping table on the format below.
ID Category_A Category_B Category_C Team
1 a b T1
2 a d T2
I have a second table which also includes Category_A, Category_B, and Category_C. I would like to join the Team value to the my second table based on the mappingtable. My problem is that when there is a blank (e.g. ID=2, Category_B) the mapping should assign the T2 to any row that contains Category_A=a and Category_C=d regardless of the value in Category_B.
Can this type of mapping be done?
Grateful for your help!
In MS Access, I think you would need something on the lines of:
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_B = t.Category_B)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Not Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_B = t.Category_B)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Null
AND m.Category_B Is Not Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_A = t.Category_A)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Null
AND m.Category_A Is Not Null
UNION ALL
SELECT t.ID, m.Team
FROM Team t
INNER JOIN Mapping m
ON (m.Category_C = t.Category_C)
AND (m.Category_B = t.Category_B)
WHERE m.Category_C Is Not Null
AND m.Category_B Is Not Null
AND m.Category_A Is Null

Problems while trying to make a query using left join and group by with having over a field of the left table on MySQL

Im trying to make some queries, first, I do this one (and works):
SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id,
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video
FROM retailer_products AS RP
LEFT JOIN groups G ON RP.id=G.retailer_product_id
INNER JOIN products P ON P.id=RP.product_id
WHERE (RP.product_id IN (1))
GROUP BY RP.id;
But when I do this one, it gaves me an empty set, the difference is that it has a "HAVING" at the end of the query over a field that may be: 0, 1 or NULL (because of the LEFT JOIN, I have no groups linked to the table retailer_product)
SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id,
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video
FROM retailer_products AS RP
LEFT JOIN groups G ON RP.id=G.retailer_product_id
INNER JOIN products P ON P.id=RP.product_id
WHERE (RP.product_id IN (1))
GROUP BY RP.id HAVING G.active=1;
So, I tried the following ways but no one works:
-- HAVING G.active=1
-- HAVING G.active=1 OR G.active=NULL
-- HAVING G.active0
What is the right way to handle this on MySQL? Thanks in advance!
The proper way to compare value with NULL is IS NULL. So you need to add HAVING G.active IS NULL OR G.active=1. Your code G.active=NULL is always NULL (false):
1 = NULL // NULL - false in conditions
NULL = NULL // NULL - false in conditions
NULL IS NULL // TRUE
Why you don't add the HAVING condition on the "Group" ON condition?
SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id,
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video
FROM retailer_products AS RP
LEFT JOIN groups G ON RP.id=G.retailer_product_id AND G.active=1
INNER JOIN products P ON P.id=RP.product_id
WHERE (RP.product_id IN (1))
GROUP BY RP.id;