简体   繁体   English

连接具有相同字段名称的表时的php关联数组

[英]Php associative array when joining tables with the same field names

I have this consult and I need the key names of the array I'm obtaining: 我有这个咨询,并且我需要获得的数组的键名:

SELECT table_1.*, table_2.* FROM... INNER JOIN...

I checked other posts with similar titles and the solution is always to use an alias. 我检查了其他标题相似的帖子,解决方案始终是使用别名。 This won't work for me because I have too many fields and it would be troublesome to write down every single one. 这对我来说是行不通的,因为我有太多字段,写下每个字段会很麻烦。

The problem is, of course, that the field names are the same in both tables. 当然,问题在于两个表中的字段名称相同。

It wouldn't be a problem if I could simply echo $arr["table_1.field_name"] but it doesn't work. 如果我可以简单地echo $arr["table_1.field_name"] ,这不会有问题,但是它不起作用。

Yes, I could use the index, like $arr[0], $arr[1], $arr[2]. 是的,我可以使用索引,例如$ arr [0],$ arr [1],$ arr [2]。 But if I change the table fields order or add new fields then I'll have to change it in the program and it does not seem like a proper solution either. 但是,如果我更改表字段的顺序或添加新字段,那么我将不得不在程序中对其进行更改,这似乎也不是正确的解决方案。

Thank you very much in advance. 提前非常感谢您。

Use AS to alias the second column. 使用AS别名第二列。 It's always the solution, because it IS the solution! 永远是解决方案,因为它是解决方案!

SELECT table1.mycolumn, table2.mycolumn AS table2column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM