MariaDB: Can I retrive duplicate column names without using 'AS'

后端 未结 1 686
余生分开走
余生分开走 2021-01-24 18:10

I have a lot of tables that use id as the primary key. When I join several tables together and select *, only one of the id columns is displayed. Whe

相关标签:
1条回答
  • 2021-01-24 18:27

    If you have multiple columns in a resultset with the same name, then even mysql returns all of them, but they will have the same name in the resultset as well, unless you use a field alias. Created an sqlfiddle to prove it here. Probably, your client library cannot distinguish between the 2 columns. This is a known issue for example with laravel.

    Both mysql and mariadb allow you to prefix the field names with table names, but, the table name is not returned as part of the field name. However, most client libraries are able to retrieve metadata for the columns in a resultset returned by mysql / mariadb, which would include the table name for non-calculated columns. See for example mysqli_fetch_field_direct() function.

    You can use the table name retrieved from metadata to determine which id column belongs to which table.

    0 讨论(0)
提交回复
热议问题