Alexandre Corvino
Alexandre Corvino

Reputation: 329

Select multiple column with join and keep the same name as in the select

I'm doing a SQL Request and I want to know if I can keep the same name as the name that I use in my select

Exemple :

SELECT users.id, users.block, users.login, users.address
FROM users 
LEFT JOIN users_info ON users.id = users_info.id

I want to get the column like

users.id | users.block | users.login | users.address

But I got it like that

id | block | login | address

And I want to know if there is another way as to do it with alias

SELECT users.id as users.id , users.block as users.block , users.login as users.login , users.address as users.address
FROM users 
LEFT JOIN users_info ON users.id = users_info.id

I'm working with pdo on php, someone know a function that can send the table name with the field when i fetchAll ?

Thanks !

Upvotes: 1

Views: 82

Answers (2)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

You have to put the name between backticks or single quotes

SQL DEMO

For example:

select version() as `mysql.version`

select version() as 'mysql.version'

Upvotes: 3

Alexandre Corvino
Alexandre Corvino

Reputation: 329

I will use fetch instead of fetchAll and PDOStatement::getColumnMeta(nbFetch)["table"] in each fetch then copy it in another array !

Thanks for help !

Upvotes: 0

Related Questions