Columns with equal names in a single query, mysql

Asked

Viewed 1,133 times

3

I’m performing a Mysql query in this style:

select contents.*, users.* FROM users, contents

Between the two tables there are columns with the same name, and when you pull them with PHP it results the first, but I want both.

Is there any way to get both data from these columns without having to change each column name? I thought I’d put a prefix in when it would work, something like that, but I don’t know if it’s functional.

  • Have many columns with equal names? the solution via sql is to give an alias (change the name manually in the query). By php see PDO deletes columns with equal names

  • There is yes renaming the field names and passing this in your sql: example select contents.id, contents.descricao as contdescricao.... and so it goes, to have the unique name for each item of your sql

  • @rray just renaming even?

  • @rray saw his option for PHP, but he will do it for all future queries? Because it occurs only in one.

  • Ai depends on how the code is organized, in case you mark that configuration before the query and end of the method remains or removes it.

  • @rray great, see, I use Mysqli, is there a similar function (from setAttribute) for it? I’m searching and still can’t find.

  • 1

    @Elaine a tip I’ll give you that I think would help you understand, if you don’t need all the fields at the junction of contents and user, the best way to optimize your SQL ( which is often a performance thief of the system ) and as it is in the response of Allan Andrade, ie specifying what you need and renaming fields that has the same name.

  • @Virgilionovic, yes I understand, but as there are more than 30 columns and all are used in PHP code, I need to try all options..

  • @Elaine I fully understand my caveat was whether you were going to use all the fields. There is also a problem there, because 30 fields would not fit a normalization there... !!! are tips ...

  • @Virgilionovic I did not understand your question, anything call me on chat. Elaine seems not that option for Mysqli.

  • 1

    @Virgilionovic I believe it is that and these two consultations are not equivalent.

Show 6 more comments

1 answer

7


You can do it like this:

select contents.nome_igual as nome1, users.nome_igual as nome2 FROM users, contents

Browser other questions tagged

You are not signed in. Login or sign up in order to post.