PDO Query on joined tables when column names are the same - mysql

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;

Related

How to display records from MYSQL DB that are not empty?

I have two tables in a database table1 and table2
First of all I have used the INNER JOIN to obtain values from table1 and table2.
SELECT table1.id, table1.name,
table2.location, table2.address
FROM table1
INNER JOIN table2 ON table1.id=table2.id;
This query works just fine but the thing is that the address column in table2 table2.address contains empty/no values. Which I don't want to fetch. How to customize this query?
Furthermore,
I also want to filter a specific location table2.location.
For example, in simple words I want that when the results are fetched the location has to be "London" and addresses that are non-empty.
SELECT table1.id, table1.name,
table2.location, table2.address
FROM table1
INNER JOIN table2 ON table1.id=table2.id
WHERE table2.address IS NOT NULL
AND table2.location = 'London'
;

How do I include table names in a MySQL query?

This seems like it would be a simple setting, but I cannot find it. I do inner join queries on tables that have similar column names. It would be nice to include the table name in the query results, so the people receiving the data can differentiate more easily. For example:
Table1:
id
name
timestamp
Table2:
id
name
timestamp
table1_id
Table3:
id
name
timestamp
table2_id
Then I tie it all together with a query:
select * from table1
inner join table2 on table1.id=table2.table1_id
inner join on table2.id=table3.table2_id;
The results have similar column header names:
id name timestamp id name timestamp table1_id id name timestamp table2_id
It's hard to tell the data apart. Of course the example query is short and silly and pointless. If I do an actual query with all the data it get more complicated. Couldn't the column header name include the table name?
table1.id table1.name table1.timestamp table2.id table2.name table2.timestamp table2.table1_id table3.id table3.name table3.timestamp table3.table2_id
You have ambiguous column names in output: table1.id, table2.id
Adding alias for columns should solve this:
SELECT table1.id as t1_id, table2.id as t2_id
Instead of writing
select * from
you can write
select table1.id as table1_id,
and do the same for the other columns so that the results set would show you the names you give yourself for each column
You can use aliases to identify columns:
SELECT table1.id AS table1_id FROM ...
But you would have to do this for each field you want to select.
try this.hope it will help you.
SELECT table1.id as t1_id, table2.table1_id as t2_id
FROM tablename
inner join table2 on table1.id=table2.table1_id
inner join table3 on table2.id=table3.table2_id;

Ambigous column name error

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

Joining multiple columns in table1 with multiple columns in table2 with additional condition MySql

I have table_1 with unique records of IDs and multiple columns of data
and
table_2 with multiple rows concerning particular ID and multiple columns. One of the column in table2 is, say, time_lapse.
I need those two tables joined with all columns saved but with only those rows from table2 with highest time_lapse value.
I was trying this way...
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where time_lapse=
(select max(time_lapse) from table2
group by id);
... but it failed.
Any suggestions for a newbie? Thank you.
You are close. But you are selecting the maximum time_lapse per id and then you act as if you had only selected one record with only one time_lapse. Use IN and have the id in the select list of your subquery:
create table as new_table
select table1.*, table2.* from
table1 left join table2
on table1.id=table2.id
where (table2.id, table2.time_lapse) in
(select id, max(time_lapse) from table2
group by id);
Then you are outer-joining table2, but want certain criteria on it in the WHERE clause. That doesn't work (as columns in outer-joined records are null).
The same query a tad prettier with a real outer join:
create table as new_table
select t1.*, t2.*
from table1 t1
left join table2 t2 on t1.id = t2.id
and (t2.id, t2.time_lapse) in
(select id, max(time_lapse) from table2 group by id);

Join 2 tables with different values

I have 2 MySQL tables: table1 and table2
The field "table1.name" has records like "category.1298" where the number after the dot comes from the field ID in table2.
I would like to join table2.ID with table1.name to identify whether table2.ID is equal to the number (after the dot) in table1.name
The question is how to eliminate the portion "category." in table1.name
substring_index is what you're looking for:
SELECT *
FROM table1
JOIN table2 ON SUBSTRING_INDEX(table1.id, '.', -1) = table2.id
Use substring_index
Select substring_index(table1.name,'.',-1) returns 1298
select *
from table1, table2
where substring_index(table1.name,'.',-1) = table2.id