<legend id='VF9vO'><style id='VF9vO'><dir id='VF9vO'><q id='VF9vO'></q></dir></style></legend>
        <tfoot id='VF9vO'></tfoot>
          <bdo id='VF9vO'></bdo><ul id='VF9vO'></ul>

        <i id='VF9vO'><tr id='VF9vO'><dt id='VF9vO'><q id='VF9vO'><span id='VF9vO'><b id='VF9vO'><form id='VF9vO'><ins id='VF9vO'></ins><ul id='VF9vO'></ul><sub id='VF9vO'></sub></form><legend id='VF9vO'></legend><bdo id='VF9vO'><pre id='VF9vO'><center id='VF9vO'></center></pre></bdo></b><th id='VF9vO'></th></span></q></dt></tr></i><div id='VF9vO'><tfoot id='VF9vO'></tfoot><dl id='VF9vO'><fieldset id='VF9vO'></fieldset></dl></div>

      1. <small id='VF9vO'></small><noframes id='VF9vO'>

        SQL 从多个表中选择 *

        时间:2023-09-20
          <tbody id='61FEg'></tbody>
          • <bdo id='61FEg'></bdo><ul id='61FEg'></ul>

                <i id='61FEg'><tr id='61FEg'><dt id='61FEg'><q id='61FEg'><span id='61FEg'><b id='61FEg'><form id='61FEg'><ins id='61FEg'></ins><ul id='61FEg'></ul><sub id='61FEg'></sub></form><legend id='61FEg'></legend><bdo id='61FEg'><pre id='61FEg'><center id='61FEg'></center></pre></bdo></b><th id='61FEg'></th></span></q></dt></tr></i><div id='61FEg'><tfoot id='61FEg'></tfoot><dl id='61FEg'><fieldset id='61FEg'></fieldset></dl></div>
                <tfoot id='61FEg'></tfoot>

                <small id='61FEg'></small><noframes id='61FEg'>

                <legend id='61FEg'><style id='61FEg'><dir id='61FEg'><q id='61FEg'></q></dir></style></legend>

                1. 本文介绍了SQL 从多个表中选择 *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用 PHP/PDO/MySQL 是否可以在对多个表进行选择并且返回的数组键是完全限定的以避免列名冲突时对列使用通配符?

                  Using PHP/PDO/MySQL is it possible to use a wildcard for the columns when a select is done on multiple tables and the returned array keys are fully qualified to avoid column name clash?

                  示例:

                  SELECT * from table1, table2;

                  给出:

                  数组键是'table1.id'、'table2.id'、'table1.name'等

                  Array keys are 'table1.id', 'table2.id', 'table1.name' etc.

                  我尝试了SELECT table1.*,table2.* ...",但返回的数组键不是完全限定的,因此具有相同名称的列发生冲突并被覆盖.

                  I tried "SELECT table1.*,table2.* ..." but the returned array keys were not fully qualified so columns with the same name clashed and were overwritten.

                  推荐答案

                  是的,你可以.最简单的方法是使用 pdo,尽管至少有一些其他扩展可以使用它.

                  Yes, you can. The easiest way is with pdo, although there's at least a few other extensions which are capable of it.

                  在 PDO 对象上设置属性,而不是 PDOStatment.

                  Set the attribute on the PDO object, not the PDOStatment.

                  $PDO->setAttribute(PDO::ATTR_FETCH_TABLE_NAMES, true);
                  

                  就是这样.然后你会得到像 $row['myTable.myColumn'] 这样的关联数组键.如果您也获取对象(例如通过 PDO::FETCH_OBJECT),它也可以工作,所以要小心,因为您需要访问像 $obj->{'myTable.myColumn'}<这样的属性/代码>

                  That's it. Then you get associative array keys like $row['myTable.myColumn']. It works if you fetch an object too (eg via PDO::FETCH_OBJECT) so beware, because you need to access the properties like $obj->{'myTable.myColumn'}

                  *手册说 PDO::ATTR_FETCH_TABLE_NAMES 属性仅受某些驱动程序支持.如果上述方法不起作用,则可能可以代替.

                  *The manual says the PDO::ATTR_FETCH_TABLE_NAMES attribute is only supported by certain drivers. If the above doesn't work, this might work instead.

                  $pdoStatement->setFetchMode(PDO::FETCH_NUM);
                  $pdoStatement->execute();
                  //build our associative array keys
                  $qualifiedColumnNames = array();
                  for ($i = 0; $i < $pdoStatement->columnCount(); $i++) {
                      $columnMeta = $pdoStatement->getColumnMeta($i);
                      $qualifiedColumnNames[] = "$columnMeta[table].$columnMeta[name]";
                  }
                  
                  //fetch results and combine with keys
                  while ($row = $pdoStatement->fetch()) {
                      $qualifiedRow = array_combine($qualifiedColumnNames, $row);
                      print_r($qualifiedRow);
                  }
                  

                  <小时>

                  相同的基本模式用于其他数据库扩展


                  Same basic pattern is used for other database extensions

                  $res = mysql_query($sql);
                  //build our associative array keys
                  $qualifiedColumnNames = array();
                  for ($i = 0; $i < mysql_num_fields($res); $i++) {
                      $columnMeta = mysql_fetch_field($res, $i);
                      $qualifiedColumnNames[] = "$columnMeta[table].$columnMeta[name]";
                  }
                  
                  //fetch results and combine with keys
                  while ($row = mysql_fetch_row($res)) {
                      $qualifiedRow = array_combine($qualifiedColumnNames, $row);
                      print_r($qualifiedRow);
                  }
                  

                  <小时>

                  mysqli

                  $res = $mysqli->query($sql);
                  //build our associative array keys
                  $qualifiedColumnNames = array();
                  foreach ($res->fetch_fields() as $columnMeta) {
                      $qualifiedColumnNames[] = "{$columnMeta->table}.{$columnMeta->name}";
                  }
                  
                  //fetch results and combine with keys
                  while ($row = $res->fetch_row()) {
                      $qualifiedRow = array_combine($qualifiedColumnNames, $row);
                      print_r($qualifiedRow);
                  }
                  

                  这也适用于表别名(在 php 7.1 中测试) - 限定列名将使用表别名.

                  This should also work with table aliases (tested in php 7.1) - the qualified column name will use the table alias.

                  这篇关于SQL 从多个表中选择 *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                2. <small id='zr5nu'></small><noframes id='zr5nu'>

                  1. <legend id='zr5nu'><style id='zr5nu'><dir id='zr5nu'><q id='zr5nu'></q></dir></style></legend>
                    <i id='zr5nu'><tr id='zr5nu'><dt id='zr5nu'><q id='zr5nu'><span id='zr5nu'><b id='zr5nu'><form id='zr5nu'><ins id='zr5nu'></ins><ul id='zr5nu'></ul><sub id='zr5nu'></sub></form><legend id='zr5nu'></legend><bdo id='zr5nu'><pre id='zr5nu'><center id='zr5nu'></center></pre></bdo></b><th id='zr5nu'></th></span></q></dt></tr></i><div id='zr5nu'><tfoot id='zr5nu'></tfoot><dl id='zr5nu'><fieldset id='zr5nu'></fieldset></dl></div>

                        <tbody id='zr5nu'></tbody>

                      • <bdo id='zr5nu'></bdo><ul id='zr5nu'></ul>
                        • <tfoot id='zr5nu'></tfoot>