选择没有AS(别名)的多个表?
我需要从多个具有很多字段的表中进行选择。我是否必须像这样手动创建查询:
table1.field1 AS table1_field1, table1.field2 AS table1_field2, ..., table2.field1 AS table2_field1, ...
或者在 MySQL 或 PHP 中是否有任何解决方法,当表中存在相同的字段名称时,它会自动处理所有这些问题?
在 PHP 中,我尝试过:
$results = $conn -> query("SELECT table1.*, table2.* FROM table1, table2 WHERE 1"); // $conn is a mysqli object
$results = $results -> fetch_array();
但是如果两个表中都有一个名为“title”的字段,则 $results['title'] 将仅包含第二个表中的记录的值。
我知道您可以使用数字索引来访问该值,但是:
- 它不可读。
- 每次表结构(例如字段顺序)发生变化时,您都必须相应地更新代码。
有什么想法吗?
I need to SELECT from more than one table that has a lot of fields. Do I have to manually fabricate the query like this:
table1.field1 AS table1_field1, table1.field2 AS table1_field2, ..., table2.field1 AS table2_field1, ...
Or is there any workaround for this in MySQL or PHP that will automatically take care of all these when there are identical field names in the tables?
In PHP, I tried:
$results = $conn -> query("SELECT table1.*, table2.* FROM table1, table2 WHERE 1"); // $conn is a mysqli object
$results = $results -> fetch_array();
But if there is a field named 'title' in both of the 2 tables, $results['title'] would just contain the value of the record from the 2nd table.
I know there are numeric indexes that you can use to access the value but:
- It's unreadable.
- You have to update the code accordingly every time the table structure such as the order of the fields changes.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,您只需要为两个表中出现的名称的字段添加别名,即使这样,您也只需要为第二列添加别名。此外,如果您只需要其中一个表中的字段,则可以仅在要检索的字段列表中包含该字段。最后,您可以使用更短的名称为每个表名称本身设置别名,然后可以在列别名中使用该名称。
所以,总而言之,你可以这样做:
Be aware that you only need to alias the fields with names that occur in both tables and even then you only need to alias the second column. Also, if you only want the field from one of the tables, you can include only that one in the list of fields to retrieve. Finally, you can alias each table name itself with a shorter name which can then be used in the column aliases.
So, all told, you can do something like:
不,SQL 中没有动态生成列别名的约定。如果您需要它们,您需要一一定义它们。
No, there's no convention in SQL to dynamically generate column aliases. If you need them, you need to define them -- one by one.
是的,有解决方法。
mysql 和 mysqli 扩展以及某些 pdo 驱动程序具有可让您获取列元数据的功能。这是一个 pdo 示例
,因此您可以使用 table1.* 并能够区分冲突的列名称。
另外,另一个有效的技巧是因为结果集中的列顺序是确定性的。
我将把代码留给您,但您可以将一行作为数组获取,并循环遍历数组键。由于它们是按顺序排列的,因此您可以假设表分隔符列后面的每一列都属于分隔符指定的表,直到遇到下一个 sep。
Yes, there are workarounds.
mysql and mysqli extensions, as well as certain pdo drivers, have functions to let you get at column meta data. Heres a pdo sample
So you can use table1.* and be able to differentiate conflicting column names.
Also, another trick that works because the column order in the result set is deterministic.
I'll leave the code up to you, but you can fetch a row as an array, and loop through the array keys. Since they will be in order, you can assume each column that follows a table separator column belongs to the table specified by the separator, until you come across the next sep.
您可以迭代数字索引并检查每个索引的名称:
如果您确实需要它,这可能是一个肮脏的解决方法,用于在上面迭代时区分您所在的表:
select 1 as _t1_ , t1.*, 1 作为来自 t1, t2 的 _t2_, t2.*
。指示表列开始位置的虚拟列。You can iterate over numeric indexes and check each index for name:
If you really need it, this could be a dirty workaround to distinguish which table you are in while iterating above:
select 1 as _t1_, t1.*, 1 as _t2_, t2.* from t1, t2
. Dummy columns indicating where table columns begin.