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;
Related
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.
I have 3 tables that i have left joined but I would like to add a field from the table 3 which is inner joined with table 2.
Table1
id
name
surname
table2_fk
Table2
id
entry_name
entry_code
table3_fk
Table3
id
type_name
type_desc
SELECT `Name`, `Type Description`
(SELECT
Table1.name AS `Name`,
Table1.surname AS `Surname`,
t2.entry_name AS `Entry`,
t2.entry_code AS `Entry Code`,
t3.type_name AS `Type Name`,
t3.type_desc AS `Type Description`
FROM Table1
LEFT JOIN table2 AS t2 ON Table1.table2_fk = t2.id
LEFT JOIN table3 AS t3 ON Table2.table3_fk = t3.id
) as subquery
My desired outcome is to have t2 inner join t3 and get field from table 1 and table 3
Based on my subquery I would like the Name and Type Description to show
If you want to INNER JOIN tables table2 and table3 and then LEFT JOIN to table1 then do this:
SELECT t1.name, t.type_desc AS `Type Description`
FROM Table1 AS t1
LEFT JOIN (
SELECT t2.id, t3.type_desc
FROM table2 AS t2
INNER JOIN table3 AS t3 ON t2.table3_fk = t3.id
) AS t ON t1.table2_fk = t2.id
But this could be perhaps expressed just as:
SELECT t1.name, t3.type_desc AS `Type Description`
FROM Table1 AS t1
LEFT JOIN table2 AS t2 ON t1.table2_fk = t2.id
LEFT JOIN table3 AS t3 ON t2.table3_fk = t3.id
I presume you are trying to left join table1 with the result of inner join between table2 and table3
select t1.name, t_res.type_desc from table1 t_1
left join (
select t_2.id, t_3.type_desc from table2 t_2
inner join table3 t_3 on t_2.table3_fk = t_3.id
) as t_res on t_1.table2_fk = t_2.id
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;
I'm trying to search a rows of table3 that have id match in a table1 OR table2 with this query
SELECT T1.*, T3.*,T2.*
FROM (
select id
from table1
where condition like '%field%') T1
inner join table3 T3
ON T3.id=T1.id
left join (
select id
from table2
where condition like '%field%') T2
ON T3.id=T2.id
If in table T1 have matches but in table2 not, the query works fine but if in table1 not have matches but have in table2 the query not show any results.
Someone can help me?
Thanks
do left join to avoid excluding rows, and add a where with the condition you need FOr readability sake,avoid right joins and jsut use proper table order
SELECT T1.*, T3.*,T2.*
FROM Table3 T3
left join (
select id
from table1
where condition like '%field%') T1
ON T3.id=T1.id
left join (
select id
from table2
where condition like '%field%') T2
ON T3.id=T2.id
where t3.id is not null or t2.id is not null
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
)
)