I want to create a select query using join on 3 or more tables. I have 3 tables namely t1
, t2
, t3
and a common column id
existing in all 3 tables. I want to select the 3 table if the id exists in the table my query is like this.
Select * from t1
inner join t2 on t1.id = t2.id
inner join t3 on t2.id = t3.id
where t1.id = 1 and t2.id = 1 and t3.id = 1
the query is returning values if the id exists in all the 3 tables. But if it is not in any table example t3 i will not return anything. I am looking for a way that if it does not exist in t3
it should i proceed to just select from t1
and t2
I think these sql wil helpful to you
Select * from t1,t2,t3
where t1.id = 1 or t2.id = 1 0r t3.id = 1
these sql also will useful to you
SELECT * FROM t1 WHERE t1.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
UNION ALL
SELECT * FROM t2 WHERE t2.id=1
Thank you.
Is this what you need?
SELECT *
FROM t1
INNER JOIN t2 on t1.id = t2.id
LEFT JOIN t3 on t2.id = t3.id
WHERE t1.id = 1 AND t2.id = 1 AND (t3.id = NULL OR t3.id = 1)
SELECT * FROM t1 JOIN t2 ON t1.id = t2.id
LEFT JOIN t2 ON t1.id= t2.id WHERE t1.id= 1
This will return null for t3
columns if id
is not present in t3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.