it's my first time here and my first question so sorry if i ask something stupid.
Well my problem is for MySQL.
Let me explain
I have 4 tables (T1,T2,T3,T4)
T1 (t1_id,external_id) here i keep the id's from T2,T3,T4 and then i have
T2 (t2_id,name,etc...)
T3 (t3_id,name,etc...)
T4 (t4_id,name,etc...)
the T1 has already some entries from these tables.
T2,T3,T4 have nothing common beetwen them but T1 has cause it keeps their ID's.
so the query is
**SELECT T1.*, T2.*, T3.*, T4.*
FROM T1
INNER JOIN T2 ON T2.t2_id=T1.external_id
INNER JOIN T3 ON T3.t2_id=T1.external_id
INNER JOIN T4 ON T4.t2_id=T1.external_id**
and it doesn't show nothing.I tried with LEFT JOIN but nothing.... but if i do this
**SELECT T1.*, T2.*
FROM T1
INNER JOIN T2 ON T2.t2_id=T1.external_id**
it works and shows data.
I hope you understand my problem.
Thanks!
Give this a try:
SELECT T1.*, T2.*, T3.*, T4.*
FROM T1
INNER JOIN T2 ON T2.t2_id=T1.external_id
INNER JOIN T3 ON T3.t3_id=T1.external_id
INNER JOIN T4 ON T4.t4_id=T1.external_id
Note that I have changed the id for T3 & T4, I assume that t2_id does not exist in them?
You should have three columns to hold each table id on T1.
T1 (T1_id, T2_id, T3_id, T4_id)
So when you do the inner join would be like this:
SELECT * FROM T1
INNER JOIN T2 ON T2.t2_id = T1.t2_id
INNER JOIN T3 ON T2.t3_id = T1.t3_id
INNER JOIN T4 ON T2.t4_id = T1.t4_id
Thank you all for your answers i used LEFT JOIN and now it's working. But the problem was that it was a capital letter on my database and both INNER & LEFT was not working.
Inner isn't correct to use cause there isn't always an entry from t2,t3,t4.
Thank you again for your time and your support!
Related
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
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.
Suppose I have following tables: T1,T2 and T3.
How could I rephrase the following query using only left joins.
Select *
From T1
Right join T2 On T1.FK2=T2.PK
Right join T3 On T1.FK3=T3.PK
Following attempt is not correct:
Select *
From T2
Left join T1 On T1.FK2=T2.PK
Left join T3 On T1.FK3=T3.PK
T3 is On the wrong Side of the join. Is the following possible:
Select *
From T2
Left join T3 On T1.FK3=T3.PK
Left join T1 On T1.FK2=T2.PK
I can't Find a way to put both tables 2 and 3 On the left Side of 1 and use the correspondent fields to join all tables? The last query uses fields of table 1 before this table is mentioned in the query.
Or something like this?
Select *
From T2
Left join (
T3 left join T1
On T1.FK3=T3.PK)
On T1.FK2=T2.PK
Apparently brackets can help to order your joins. I wonder if this is really documented, i've found Nothing at first glance in the mysql docs.
Following query is correct and does not have any subqueries:
Select T1.Id Ida, t2.id idb, T3.id idc FROM T3
LEFT JOIN
(T2
LEFT JOIN T1 ON (T1.ID = T2.ID))
ON (T1.ID= T3.ID);
You need to use a subquery to first join t1 with t2 and then join the result with t3:
SELECT T.ID1 ID1, T.ID2 ID2, T3.ID ID3 FROM T3
LEFT JOIN
(SELECT T1.ID ID1, T2.ID ID2 FROM T2
LEFT JOIN T1 ON (T1.ID = T2.ID)) T
ON (T.ID1 = T3.ID);
SQL Fiddle
The first way is just to reverse the order that the tables are mentioned:
Select *
from t3 left outer join
t2
on T1.FK3 = T3.PK left outer join
t1
on T1.FK2 = T2.PK
But this won't work, because the first condition is on t1 and not t2. And t2 hasn't yet been defined.
When working with chains of tables in left or right outer joins, only the first (or last) tables are important, because they "drive" the query. "Drive" in the sense that they provide all the values even when there are no matches. So, the following should do what you want:
Select *
from t3 left outer join
t1
on T1.FK3 = T3.PK left outer join
t2
on T1.FK2 = T2.PK;
Is it possible to select multiple tables and make inner join on one of those tables? e.g.
SELECT *
FROM table1 AS t1, table2 AS t2, table3 AS t3
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
This paricular case is causing me a problem. it gives me an error - Unknown column "t1.row3" in 'on clause'. I don't know if it's possible to select multiple tables but make inner join on one of those tables.
The JOIN operand has higher precedence than comma , operand, so the join is effectively treated as
t1, t2, (t3, t4, t5 ON ... )
Put parentheses around t1, t2, t3.
SELECT *
FROM ( table1 AS t1, table2 AS t2, table3 AS t3 )
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
You can also write your query as:
SELECT *
FROM table1 AS t1
INNER JOIN table2 AS t2
INNER JOIN table3 AS t3
INNER JOIN table4 AS t4 ON t1.row3 = t4.row3
INNER JOIN table5 AS t5 ON t1.row4 = t5.row4
WHERE ...
because comma is equivalent to INNER JOIN without a join condition.
I want to merge three tables together as shown here:
Basically I want to include the items from all three tables T1, T2 and T3 and have them merged as shown in the result table. I tried something like this:
SELECT T1.user, T2.tid, T2.name, T3.type, T1.mid
FROM T1
LEFT JOIN T2 ON T1.mid = T2.mid
LEFT JOIN T3 ON T2.tid = T3.tid
GROUP BY T1.user;
But it does not seem to have worked. It does show the results but only unique values. In the result if user is johny, it will only show the first value and ignore the second, though it should be in the result table.
Is there something I am missing?
The Group is not necessary if you want to see all results for each of the users. Otherwise it will hide some of the rows and show just one per user.
First join T1 Right to T2 than Left Join to T3. This is good practice if there is element from T1 that has no connection with element from T3 to prevent showing NULL result for T£ fields.
SELECT T1.user, T2.tid, T2.name, T3.type, T1.mid
FROM T1
RIGHT JOIN T2 ON T1.mid = T2.mid
LEFT JOIN T3 ON T2.tid = T3.tid;
Get rid of the "Group By" part. This should fix your problem.
Eliminate the GROUP BY. There's no need for it in this query.
SELECT T1.user, T2.tid, T2.name, T3.type, T1.mid
FROM T1
LEFT JOIN T2 ON T1.mid = T2.mid
LEFT JOIN T3 ON T2.tid = T3.tid;