Selecting from multiple tables with LEFT JOIN - mysql

I have 3 tables
t1 (select these records)
-------------
id
offer_id
business_id
t2 (offer details)
-------------
id
offer_details
business_id
t3 (business details)
-------------
id
business_name
I need to select all records from t1 and add information from t2 and t3. Seems basic but I can't seem to be able to get it right -- must be the heat.
SELECT t2.offer_details, t3.business_name
FROM t2
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)

should be
SELECT t2.offer_details, t3.business_name
FROM t1
LEFT JOIN t1 ON (t1.offer_id = t2.id)
LEFT JOIN t3 ON (t1.business_id = t3.id)
Your lead table is t1 and the join should be based on this table

How about this
Select t2.offer_details, t3.business_name
From t1
Left Join t2 ON (t1.offer_id = t2.id)
Left Join t3 ON (t1.business_id = t3.id)

If you want all records from t1, add t1.* on your select part. Assuming that all IDs in t1 exists in the other 2 tables
SELECT
t1.*, t2.offer_details, t3.business_name
FROM
t1
JOIN t2 ON t2.id = t1.offer_id
JOIN t3 ON t3.id = t1.business_id
Modify to LEFT JOIN if the IDs in t1 may be missing in t2 or t3.

Related

mySQL group two INNER JOINs

I basically want to join the result of two INNER JOINs.
On this scheme I want to get the three arrows results combined.
I've tried INNER / LEFT combinations but it doesn't do the trick.
I think a nested request could be the solution but how ?
Thanks
The answer was actually simple : UNION
SELECT t1.*
FROM
(SELECT t1.*
FROM table1 t1 JOIN table2 t2 ON t2.id = i.client_id
UNION
SELECT t1.*
FROM t1 t1 JOIN table3 t3 ON t1.id = t3.client_id) as q1
;
I'd use logic to express the condition T1.id exists in T2 or T3 more directly, and certainly avoid use of DISTINCT or UNION.
Options could be to use EXISTS directly (As this is immure to the possibility of duplication cause by 1:many joins)...
SELECT
t1.*
FROM
table1 t1
WHERE
EXISTS (SELECT * FROM table2 t2 WHERE t2.t1_id = t1.id)
OR
EXISTS (SELECT * FROM table3 t3 WHERE t3.t1_id = t1.id)
Or to LEFT JOIN twice and then exclude unwanted rows. (This assumes that the joins are never 1:many, which would introduce duplication, and the unwanted need for a DISTINCT.)
SELECT
t1.*
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.id = t2.t1_id
LEFT JOIN
table3 t3
ON t1.id = t3.t1_id
WHERE
t2.t1_id IS NOT NULL
OR
t3.t1_id IS NOT NULL

Inner join one to many unique_id between two table

one table t1 has one unique id u1
another table t2 has three unique_id u2a, u2b, u2c
How to make an inner join between two tables so that
u1 of table t1 join either u2a, u2b or u2c of table t2.
I don't think this is a best practice but try:
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/116
If you want the name in order you can add order by name desc; in the end of the query.
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c
order by name desc;

mysql foreign key foriegn key relationship

Say, myself having three MySQL tables as follows.
Table 1 contains table1id, value columns.
Table 2 contains table2id, value, table1id (as FK) columns.
Table 3 contains table3id, value, table1id (as FK) columns.
Then is the following relationship valid?
select * from table1 t1 inner join table2 t2 on t1.table1id = t2.table1id
Yes, It is possible.
This one is Joining for teable1 and table2
select * from test1 t1 inner join test2 t2 on t1.id = t2.id;
This one is joining all three tables,
SELECT * FROM test1 t1
INNER JOIN test2 t2 ON t1.id = t2.id
INNER JOIN test3 t3 ON t1.id = t3.id;
Output: ONLINE DEMO HERE
Try this
SELECT * FROM
table1 t1 INNER JOIN table2 t2
ON t1.table1id = t2.table2id
INNER JOIN table3 t3
ON t1.table1id = t3.table3id
You can also write it this way:
SELECT * FROM table1 t1, table2 t2, table3 t3
WHERE (t1.table1id=t2.table2id) AND (t1.table1id=t3.table3id);
** If you want to join only the first 2 tables--Use the code until the AND
*** If you want to join all the tables-- Use the entire code.

MySQL - replace LEFT JOIN inplace of NOT IN

Whats the right MySQL query with two LEFT JOINs between three tables?
SELECT COUNT(1) FROM TABLE1 WHERE T1_ID NOT IN (
SELECT T2.T2_ID FROM TABLE2 T2 LEFT JOIN
TABLE3 T3 ON T2.T2_ID=T3.T3_ID WHERE T3.T3_ID IS NULL )
Something like
SELECT COUNT(1) FROM TABLE1 T1 LEFT JOIN TABLE2 T2 ON T1.T1_ID=T2.T2_ID
LEFT JOIN TABLE3 T3 ON T2.T2_ID=T3.T3_ID WHERE T2.T2_ID IS NULL AND
T3.T3_ID IS NULL
For performance I would do something like
SELECT
COUNT(1)
FROM
TABLE1
LEFT JOIN (
SELECT
T2.T2_ID id
FROM
TABLE2 T2
LEFT JOIN TABLE3 T3 ON
T2.T2_ID=T3.T3_ID
WHERE
T3.T3_ID IS NULL
) t1 ON
t1.id = table1.t1_id
WHERE
t1 is null;

Select in where clause, access to current parent select columns

I have a query which contains a select statement in it's where clause. My question is now, how can I access the parent's select's data.
Example:
select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR
(not exists(select * from TABLE_3 as t3
inner join TABLE_1 ON t3.t1_id = t1.id
The last line is where the error occurs: t1.id is not a column.
How can I access the current value from the table t1?
I'm using MySql 5.1
SELECT
*
FROM
TABLE_1 as t1
INNER JOIN TABLE_2 as t2 ON
t2.PK = t1.FK --Whatever your keys are
WHERE
(... and ...)
OR
(
NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
)
First of all, you need to declare what you will JOIN TABLE_2 on TABLE_1.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id = t2.t1_id is just an example, you will need to decide which columns you wish you join on. Then in your WHERE clause, you do not need to INNER JOIN on TABLE_1 again as you are already selecting from it.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR
(
NOT EXISTS
(
SELECT *
FROM TABLE_3 AS t3
WHERE t3.t1_id = t1.id
)
)