SQL Joining Tables - sql

I am trying to to join two tables in order to replace the numeric values of one from the other, the problem is that i have two columns in the same table. See this example to make it clear :
Table_1 t1_ID INT, t1_Name VARCHAR(20)
Table2 t2_ID INT , ONE Table_1 , TWO Table_1
in table 2 i store the id and i want to make a join in order to replace these id's with t1_Names.
I have tried this structure but it gives wrong answers
Select *
FROM table1
JOIN table2 ON table1.id=table2.table1_id
JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id

You can join the same table twice, but you need to give this table different names (alias name)
Select t2.id, t1_1.name, t1_2.name
FROM table2 t2
JOIN table1 t1_1 ON t1_1.id = table2.table1_id
JOIN table1 t1_2 ON t1_2.id = table2.table2_id

Related

SQL joining of two different queries

Table1: column is( ID)
Table2: column is (NAME)
QUERY:
SELECT ID FROM TABLE1
SELECT NAME FROM TABLE2
NOW, I want output side by side , is there any possibility
Try this....
if this is without any condition
select id, name from table1, table2
I woder whether you mean CROSS JOIN
select id, name
from table1
cross join table2
You need to do an JOIN between tables on a value that exists in both tables. An INNER JOIN only takes rows that has a match in both tables.
Example:
SELECT tb1.id, tb2.name FROM table1 AS tb1 INNER JOIN table2 AS tb2 ON tb1.id = tb2.table1_id;

Adding rows of one table to rows of another table where two tables are matched by ID

In an Access 2013 database, I have a table t1 and another table t2. They both have the same number of columns and column names are also the same. Table t2 have a number of overlaps with id variable of table t1. I am trying to make a new table t3 where I add all the rows of t1 and only those rows of t2 that are not matched by an id variable present in both the tables t1 and t2. I used something like
Create Table t3 As Select * From (Select t1.* From t1 Inner Join t2 on t1.ID_Number = t2. ID_Number)
This throws syntax error. However, even if it worked this will select those rows that matches ID_Number in both the tables. I have tried various other codes and browsed through many other relevant stackoverflow post but could not resolve it.
try this :
SELECT t1.*
INTO t3
FROM t1
INNER JOIN t2
ON t1.ID_Number = t2.ID_Number
I am not sure about Access syntax but can this 2-step solution work?
select t1.* into t3 from t1 where t1.ID_Number not in (select t2.ID_Number from t2)
select t2.* into t3 from t2 where t2.ID_Number not in (select t1.ID_Number from t1)

Retrieving values from another table which has its relationship to the first table defined by a third table

Right now I have
SELECT
table1.field1 AS whatever
, table2.field2 AS stuff
FROM
table1
INNER JOIN table2 ON table1.goId = table2.Id
I need to be able to select a field3 which is located in a table3. Thing is, table1 has no direct foreign key pointing to table3, but it DOES have foreign key that points to a table which has another foreign key pointing to table3. How do I make the join between these 3 tables?
Given the lack of database structure provided the following could be used
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4 ON table1.columnName1 = table4.columnName1
JOIN table3 ON table4.columnName4 = table3.columnName4
OR you could nest the third table
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4
JOIN table3 ON table4.columnName4 = table3.columnName4
ON table1.columnName1 = table4.columnName1
Say that fourth table is table2.
SELECT
table3.column_You_want
FROM
table3
INNER JOIN table2 ON table2.column_linked_to_table3 = table3.column_name_linked_to_table2
INNER JOIN table1 ON table2.column_linked_to_table1 = table1.column_linked_to_table2

Selecting a mapping table with fields from two other tables

I have a mapping table referring to ids from two different tables. I would like to select the mapping table with each id being replaced by another field in the respective table.
To be a little more explicit: there are three tables with two columns each:
Table1 has a id (the primary key) and field1
Table2 has a id (the primary key) and field2
Table3 (the mapping table) has fields Table1_id (which takes values in Table1.id) and Table2_id (which takes values in Table2.id)
What I want is to get the content of Table3 with Table1.field1 and Table2.field2 as columns.
I know how to replace one of the columns in the mapping table with another column of one of the other tables, by using a inner join:
SELECT Table1.field1, Table3.Table2_id
FROM Table1
INNER JOIN Table3
ON Table1.id=Table3.Table1_id;
however I don't know how to do basically the same thing with both columns.
If i understood correctly you are trying to get field1 from Table1 and field2 from table 2. If so you just need to join the three tables
SELECT a.field1, c.field2
FROM Table1 a
INNER JOIN Table3 b
ON a.id=b.Table1_id
INNER JOIN Table2 c
ON b.Table2_id = c.id
Do another join.
SELECT Table1.field1, Table2.field
FROM Table1
INNER JOIN Table3
ON Table1.id = Table3.Table1_id
INNER JOIN Table 2
ON Table2.id = table3.table2_id;
You just need to join all three tables; something like
SELECT Table1.Field1, Table2.Field2
FROM Table3
JOIN Table1 ON Table1.Id = Table3.Table1_id
JOIN Table2 ON Table2.Id = Table3.Table2_id
I do not know if I understood correctly what you want to achieve but this should get you results from both tables joined by thier keys
SELECT Table1.field1, Table2.field2
FROM Table1
INNER JOIN Table3 ON Table1.id = Table3.Table1_id;
INNER JOIN Table2 ON Table2.id = Table3.Table2_id;

Join Unlike Tables

I have 2 unlike tables and a large set of subqueries that have a key for each of those two tables. I need to join the two tables to each subquery.
Table 1
Table1ID
Table 2
Table2ID
Subqueries
Table1ID
Table2ID
Is there any way to join everything together?
I have tried something similar to
SELECT Table1.Table1ID, Table2.Table2ID
FROM Table1, Table2
LEFT JOIN (SELECT Table1ID, Table2ID FROM ....) q1 ON Table1.Table1ID = q1.Table1ID AND Table2.Table2ID = q1.Table2ID
...
This following query will select all fields from a join of all three tables on the respective table IDs:
SELECT *
FROM Table1 t1
INNER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
INNER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id
If you need absolutely all records from both Table1 and Table2, whether they are joined via the Subqueries table, then you can change the join to FULL OUTER:
SELECT *
FROM Table1 t1
FULL OUTER JOIN Subqueries s
ON t1.Tabl1Id = s.Table1Id
FULL OUTER JOIN Table2 t2
ON s.Tabl2Id = ts.Table2Id