mysql foreign key foriegn key relationship - mysql

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.

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

Selecting from multiple tables with LEFT JOIN

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.

Retrieve data from two tables sql

I am having 2 tables named table1, table2.
table1 columns are t1_id, t1_name, t1_status.
table2 columns are t1_id, t2_id, t2_name, t2_status.
When I do Some operation in frontend t1_id will be inserted into table2.
What I need is I want t1_id(s) from table1 which are not alloted or inserted in table2.
I've tried this query:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id != t2.t1_id
The problem with query is when all t1_id(s) are inserted into table2, then all t1_id(s) are showing again.
How to resolve this issue? (I'am new to sql so please don't consider my mistakes.)
If i understand your question right, this must b the query you need:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
where t2.t1_id is null
Use is null to get rows from table1 which don't have any associations in table2
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
WHERE t2.t1_id IS NULL
I think this answers your questions: Select rows which are not present in other table
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 --it's not so relevant what you put here
FROM table2 t2
WHERE t1.t1_id = t2.t1_id
)
I hope this helps. ;-)

One SQL statement for two Foreign Keys

I have two tables. The first table contains ID, First_Name and Last_Name.
The 2nd table contains two foreign key fields containing different ID's of the first table.
I want to be able to run a SQL query that gets reults of the 2nd table which then grabs the First_Name of each member based on the two different foreign keys.
How would I go about doing this?
select t2.*, t1a.firstname, t1b.firstname
from table2 t2
left join table1 t1a on t2.fk1 = t1a.id
left join table1 t1b on t2.fk2 = t1b.id
Suppose the second table has fields as such
userid, supervisorid ( both referring to the Id column of the first table )
you may write join to get the value like this
SELECT t2.*, ID, firstname, lastname FROM table 2 t2
LEFT OUTER JOIN table 1 t1 ON
t2.userid = t1.id
OR t2.supervisorid = t1.id
I think correct sql would be below one using OR condition in outer join or using union
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id1
UNION
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id0
SELECT t1.id, t1.name from table2 t2 LEFT OUTER JOIN table1 t2 ON t1.id = t2.id or t1.id1 = t2.id0

SQL Server - exclude data where there's no connection to second table

The example below shows the result for every Name that has a connection to Table2 (Table1 TId is PK, and TId in Table2 is the FK).
SELECT T1.Name, T1.Address
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.TId = T2.TId;
I want a list of all Names from Table1 that have NO corresponding row in Table2. The other way around so to speak. How could this be done?
You need to use an Outer Join as shown below:
SELECT T1.Name, T1.Address
FROM Table1 AS T1
LEFT OUTER JOIN Table2 AS T2 ON T1.TId = T2.TId
WHERE T2.TId IS NULL