Selecting a mapping table with fields from two other tables - sql

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;

Related

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

SQL Select with join query works but delete does not work

I have two tables Table1 and Table2. I compared thes tables using the query below. SELECT
Select Table1.ID
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
and got 1508 records that exist in Table1 but not in Table2.
Now I wanted to delete these records in Table2. This is the code I used below
DELETE Table1.*
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.ID =Table2.ID
WHERE Table2.ID IS NULL
This is the error I am get Could not delete from specified tables. I realize something is wrong with my sql, but where. I thought this DELETE was specifying a table not tables.
Try this:
DELETE FROM Table1
WHERE ID NOT IN
(
SELECT ID
FROM table2
)

Joining empty table to return all rows

I have a table (Table1) which has a composite primary key(Column1 + Column2). I am using it as a foreign key in another table (Table2).
Now I want to a SELECT statement to select all records from Table1 and Table2.
But its returning me 0 rows, because table2 is Empty. I want all records from table1 and if it does not exist in table2, value of Columns in Table2 should be null.
I know, I only need to Join it. But I am not getting it right.
Thanks
SELECT * FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id = T2.FK
FK is your foreign key on the second table.
A Left Join will return all rows from table1 even if they don't exist in table2.
You need an outer join
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.column1 = table2.column1
AND table1.column2 = table2.column2
Left means preserve all rows from the left (first) table in the query.
You need a LEFT JOIN
SELECT Table1.*, Table2.*
FROM Table1
LEFT JOIN Table2 ON Table1.Column1 = Table2.Column2
Try that out.
Use LEFT JOIN for join you tables. See SQL SERVER JOINS to understand the concept.

Selecting records based on two other tables

I have a query with inner join to another table, with this I want also include records which are contained in another column.
Example:
select name, address from table1
inner join table2 on table1.id = table2.id
With this, I want to also include rows which are having table1.recno = (1,2,4).
How could I write query for that?
One option I know is to use the IN keyword instead of the first table join. But our client doesn't want to use the IN keyword.
Use a left join and then use the WHERE clause to filter out the rows that you need.
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL OR table1.ID In (1,2,4)
Or if you want to avoid an innocuous IN for silly reasons, use:
select name, address
from table1
left join table2 on table1.id = table2.id
where
table2.id IS NOT NULL
OR table1.ID = 1
OR table1.ID = 2
OR table1.ID = 4

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