How do I INNER JOIN a table that contains 2 foreign keys as its primary keys?
CREATE TABLE table1 (table1ID CHAR(4));
CREATE TABLE MEM_INSTR (table2ID CHAR(4));
CREATE TABLE table3 (table1ID CHAR(4), table2ID CHAR(4));
Assuming you want to just join everything together as keys suggest...
SELECT *
FROM table1
INNER JOIN table3 on table3.table1ID = table1.table1ID
INNER JOIN MEM_INSTR on MEM_INSTR.table2ID = table3.table2ID
But let's say that you have this scenario.
CREATE TABLE Table1 (
Table1ID NUMBER,
Generation NUMBER,
...
);
CREATE TABLE Table2 (
Table2ID NUMBER,
Table1ID NUMBER,
Table1Generation NUMBER,
...
);
Let's say for argument's sake that Table1 can have multiple records with the same Table1ID, and Generation is used as a secondary key. And you need to join a Table2 record to the correct single Table1 record. You can expand the ON clause the same way you would expand a WHERE clause.
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.table1id = t1.table1id
AND t2.table1generation = t1.generation
You join it like you usually do, nothing really special about that. So you go something like this:
SELECT ...
FROM table1
INNER JOIN table3 ON table3.table1ID = table1.table1ID
INNER JOIN MEM_INSTR ON MEM_INSTR.table2ID = table3.table2ID
Related
I'm trying to make a new table from the inner join results of three other tables (t1, t2, t3)
INSERT INTO new_table
SELECT
t1.application_id,
t1.text,
t2.names,
t2.title,
t3.org_name, t3.project_start, t3.project_end, t3.keywords
FROM t1
INNER JOIN t2 ON t1.application_id = t2.application_id
INNER JOIN t3 ON t1.application_id = t3.application_id;
but keep getting a ER_DUP_ENTRY: Duplicate entry '9481301' for key 'PRIMARY'error. Each table should have id as a primary key so I'm confused why this is happening - how would one find and delete all the duplicates?
I bet your new_table is defined with an AUTOINCREMENT primary key.
To make your INSERT work correctly you'll need to let MySQL set that value rather than providing it from your SELECT. Something like this may work for you.
INSERT INTO new_table
(text, names, title, org_name, project_start, project_end, keywords)
SELECT
t1.text,
t2.names,
t2.title,
t3.org_name, t3.project_start, t3.project_end, t3.keywords
FROM t1
INNER JOIN t2 ON t1.application_id = t2.application_id
INNER JOIN t3 ON t1.application_id = t3.application_id;
You do this by leaving the id value out of your SELECT, and by enumerating the columns you want to INSERT.
(This is a guess: you didn't show us your table definition.)
I have an issue with select command from 2 tables.
So I have table1 with:
table1_id = int pk;
table1_name;
table1_surname;
table1_age;
table1_address;
table1_city;
And table2 with:
table2_id int pk
table1_id int fk references table1.table1_id;
table3_id;
table2_description;
When I write the following select statement, I get ambigous column name table1.table1_name error:
SELECT table2.table2_id, table2.table1_id, table1.table1_name, table2.table2_description
from table1,
table2 inner join
table1
on table2.table1_id = table1.table1_id;
Honestly I do not understand what is wrong about it?
If i understood correctly, you have problem in below line
from table1, table2
In the above code you are using a CROSS JOIN between table2 and table1 which is not required in your case.
Change your query like following.
SELECT table2.table2_id, table2.table1_id, table1.table1_name, table2.table2_description
from table2
inner join table1 on table2.table1_id = table1.table1_id;
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax:
select t2.table2_id, t2.table1_id, t1.table1_name, t2.table2_description
from table1 t1 join
table2 t2
on t2.table1_id = t1.table1_id;
The problem with your query is that you have two references to table1 because of the comma. You have mentioned the table twice. Hence, when you reference the column, the engine doesn't know what you are referring to. Your version is equivalent to:
from table1 cross join
table2 join
table1
on table2.table1_id = table1.table1_id
table1 appears twice, so any reference to it is ambiguous.
You will notice that I also added table aliases to the query. Table aliases make the query easier to write and to read.
Remove table1, just after from ( mixed old type "comma" and modern join syntaxes)
Use like the following :
SELECT t2.table2_id, t2.table1_id, t1.table1_name, t2.table2_description
FROM table2 t2 INNER JOIN table1 t1 ON ( t2.table1_id = t1.table1_id ) ;
SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.
I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".
I have 3 tables that look like this:
Table1:
PersonSSN
NumberOfCars
Table2:
PersonSSN
NumberOfPhones
Table3:
PersonName
PersonSSN
Both Table 1 and Table2 have a foreign key reference to Table3 on PersonSSN.
I need to join these in such a way that I get:
PersonName NumberOfPhones NumberOfCars
Here are some conditions that apply to the join:
If a person has an entry in both Table1 and Table2 I see all 3 fields populated for him.
If a person has an entry in Table1 and not in Table2 he should still show up but with NumberOfPhones set to 0.
Likewise, if a person has an entry in Table2 and not in Table1 he should still show up but with NumberOfCars set to 0.
Can this be achieved in one query ? If yes what should the query be ?
This is a left outer join query:
select t3.name, coalesce(t1.NumberOfPhones, 0), coalesce(t2.NumberOfCars, 0)
from table3 t3 left outer join
table1 t1
on t3.ssn = t1.ssn left outer join
table2 t2
on t3.ssn = t2.ssn;