选择没有AS(别名)的多个表?

发布于 2024-10-11 07:40:35 字数 623 浏览 2 评论 0原文

我需要从多个具有很多字段的表中进行选择。我是否必须像这样手动创建查询:

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'] 将仅包含第二个表中的记录的值。

我知道您可以使用数字索引来访问该值,但是:

  1. 它不可读。
  2. 每次表结构(例如字段顺序)发生变化时,您都必须相应地更新代码。

有什么想法吗?

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:

  1. It's unreadable.
  2. You have to update the code accordingly every time the table structure such as the order of the fields changes.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很酷不放纵 2024-10-18 07:40:36

请注意,您只需要为两个表中出现的名称的字段添加别名,即使这样,您也只需要为第二列添加别名。此外,如果您只需要其中一个表中的字段,则可以仅在要检索的字段列表中包含该字段。最后,您可以使用更短的名称为每个表名称本身设置别名,然后可以在列别名中使用该名称。

所以,总而言之,你可以这样做:

 SELECT t1.id, t1.title, t2.title as second_title, t2.somethingelse
    FROM  table1 t1 INNER JOIN table2 t2. . .

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:

 SELECT t1.id, t1.title, t2.title as second_title, t2.somethingelse
    FROM  table1 t1 INNER JOIN table2 t2. . .
水染的天色ゝ 2024-10-18 07:40:36

不,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.

人│生佛魔见 2024-10-18 07:40:36

是的,有解决方法。
mysql 和 mysqli 扩展以及某些 pdo 驱动程序具有可让您获取列元数据的功能。这是一个 pdo 示例

$stmt->execute();
$qualifiedColumnNames = array();
for ($i = 0; $i < $stmt->columnCount(); $i++) {
    $columnMeta = $stmt->getColumnMeta($i);
    $qualifiedColumnNames[] = "$columnMeta[table].$columnMeta[name]";
}

print_r(array_combine($qualifiedColumnNames, $stmt->fetch(PDO::FETCH_NUM)));

,因此您可以使用 table1.* 并能够区分冲突的列名称。

另外,另一个有效的技巧是因为结果集中的列顺序是确定性的。

select 'table1' table_sep1
       table1.*
       'table1' table_sep2
       table2.*

我将把代码留给您,但您可以将一行作为数组获取,并循环遍历数组键。由于它们是按顺序排列的,因此您可以假设表分隔符列后面的每一列都属于分隔符指定的表,直到遇到下一个 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

$stmt->execute();
$qualifiedColumnNames = array();
for ($i = 0; $i < $stmt->columnCount(); $i++) {
    $columnMeta = $stmt->getColumnMeta($i);
    $qualifiedColumnNames[] = "$columnMeta[table].$columnMeta[name]";
}

print_r(array_combine($qualifiedColumnNames, $stmt->fetch(PDO::FETCH_NUM)));

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.

select 'table1' table_sep1
       table1.*
       'table1' table_sep2
       table2.*

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.

°如果伤别离去 2024-10-18 07:40:36

您可以迭代数字索引并检查每个索引的名称:

$result = mysql_query('select t1.*, t2.* from t1, t2');
$num = mysql_num_fields($result);
for ($i = 0; $i < $num; $i++) {
    $column_name = mysql_field_name($result, $i);
}

如果您确实需要它,这可能是一个肮脏的解决方法,用于在上面迭代时区分您所在的表:select 1 as _t1_ , t1.*, 1 作为来自 t1, t2 的 _t2_, t2.*。指示表列开始位置的虚拟列。

You can iterate over numeric indexes and check each index for name:

$result = mysql_query('select t1.*, t2.* from t1, t2');
$num = mysql_num_fields($result);
for ($i = 0; $i < $num; $i++) {
    $column_name = mysql_field_name($result, $i);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文