Ambigous column name error - mysql

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 ) ;

Related

Join two tables based on a view, sql

I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.

Pull Data from 3 mySQL Tables

I have a query that selects all the data from one table and specific columns from another where two columns are equal and another column equals a specific value.
SELECT table1.*, table2.column1, table2.column2 FROM table1
INNER JOIN table2 ON table1.column3=table2.column3
WHERE table1.column1='foo';
Is it possible to pull table3.column1 & table3.column2 from a table3 where table3.column3=table1.column3 AND table3.column4='bar' ?
The thing that makes it more complex is that the data in table3 is optional and may not always exist; however I still want the query to return table1 & table2's data but with table3.column1 & table3.column2 just being presented as NULL or EMPTY...
Im struggling to get my head around it myself, and any insight or assistance would be greatly appreciated.
Use left join:
SELECT table1.*, table2.column1, table2.column2, table3.column1, table3.column2
FROM table1
INNER JOIN table2
ON table1.column3 = table2.column3
LEFT JOIN table3
ON table3.column3 = table1.column3 AND table3.column4 = 'bar'
WHERE table1.column1 = 'foo';
When there is no corresponding record in table3 table3.column1 and table3.column2 will be null
And if column1 and column2 names are not unique column names in select clause you need to give them an alias
select t1.col1, t2.col2, t3.col3
from table1 as t1
inner join table2 as t2 on t2.col3.t1.col2
left join table3 as t3 on t3.col3.t2.col2
where t1.col1 = 'test';
it can be done like this.

MySQL JOIN from one of the two table based on IF condition

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.

SQL - show results from table without values from another table

I need to show only results which are in Table1 and Table2 but are not in Table3. Basically, it should be something like TABLE1, Table2 except INNER JOIN between (TABLE1, Table2) and TABLE3.
Should looks like this - On left side Table1 and Table2, on right side Table3
Now I have this:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
Can somebody help me please? Thanks a lot.
There are a couple different ways to do this. I believe mysql does better with the outer join/null approach:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
This assume table3 shares the same structure as the other 2 tables.
I would approach the problem almost directly as you write it, using exists and not exists:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
One advantage of exists/not exists over other approaches involves duplicates. If one of the tables (say table1) has not duplicates, but the others might, there is no need to remove duplicates in the resulting data set.

PDO Query on joined tables when column names are the same

I'm running a query via PDO on a pair of joined tables like so:
SELECT table1.id, table2.id, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
Both tables have an id column so when I run fetchAll() the associative array only contains one id field. This is because the first is overwritten by the second.
Is there a way to obtain both id fields? Perhaps by having the table name included in the array keys...
use aliases
SELECT table1.id as t1id, table2.id as t2id
--etc.
try this
SELECT table1.id AS idtable1, table2.id AS idtable2, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;