• <legend id='rD2SG'><style id='rD2SG'><dir id='rD2SG'><q id='rD2SG'></q></dir></style></legend>

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

      <small id='rD2SG'></small><noframes id='rD2SG'>

        <bdo id='rD2SG'></bdo><ul id='rD2SG'></ul>

        PHP/MySQL:在无别名的联接查询中获取多个同名列?

        PHP/MySQL: Getting Multiple Columns With the Same Name in Join Query Without Aliases?(PHP/MySQL:在无别名的联接查询中获取多个同名列?)
        <legend id='y9WQH'><style id='y9WQH'><dir id='y9WQH'><q id='y9WQH'></q></dir></style></legend>
          • <bdo id='y9WQH'></bdo><ul id='y9WQH'></ul>
            <tfoot id='y9WQH'></tfoot>

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

                    <tbody id='y9WQH'></tbody>
                1. <small id='y9WQH'></small><noframes id='y9WQH'>

                2. 本文介绍了PHP/MySQL:在无别名的联接查询中获取多个同名列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两张桌子.一个用于用户,一个用于帖子.users 表有以下字段:

                  I have two tables. One for users and one for posts. The users table has the following fields:

                  id, username, password, created_at, modified_at
                  

                  posts 表有以下字段:

                  The posts table has the following fields:

                  id, user_id, title, body, created_at, modified_at
                  

                  当我使用如下查询时:

                  SELECT * FROM `users` LEFT OUTER JOIN `posts` ON users.id=posts.user_id
                  

                  并使用 PDO 获取结果:

                  And fetch the results using PDO:

                  $sth = $this->$default_connection->prepare($query);
                  $sth->execute();
                  $sth->fetchAll(PDO::FETCH_ASSOC);
                  

                  返回的数组会覆盖所有具有相同名称的列,例如 id、created_at、modified_at,如下所示:

                  The returned array overwrites all the columns with the same names like id, created_at, modified_at like this:

                   Array
                  (
                      [0] => Array
                          (
                              [id] => 1
                              [username] => johnSmith
                              [password] => 2348r7edf8s79aa0230
                              [created_at] => 0000-00-00 00:00:00
                              [modified_at] => 0000-00-00 00:00:00
                              [user_id] => 18
                              [title] => First Post
                              [body] => Just testing...
                          )
                  )
                  

                  用户的 id 字段未显示,而是被帖子的 id 覆盖.created_at & 也是如此.modified_at 字段.

                  The id field for the user is not shown, instead overwritten by the id of the post. Same goes for the created_at & modified_at fields.

                  我相信我可以通过使用别名或类似的东西来解决这个问题:

                  I believe I can solve this problem by either using aliases or something like this:

                      SELECT 
                         users.id, 
                         users.username, 
                         users.password, 
                         users.created_at, 
                         users.modified_at, 
                         posts.id AS postId, 
                         posts.user_id, 
                         posts.title, 
                         posts.created_at AS postCreatedAt, 
                         posts.modified_at AS postModifiedAt
                     FROM `users`
                         LEFT OUTER JOIN `posts` ON (users.id=postId)
                  

                  或类似的东西,但我想知道是否有一种方法可以做到这一点,而无需每次我想区分两个同名的列时手动写出每一列的名称或别名?是否可以做类似的事情:

                  Or something similar, but I was wondering if there was a way to do this without manually writing out the name of every single column or an alias for every time I want to distinguish between two columns of the same name? Is it possible to do something like:

                  SELECT users.* AS User and posts.* AS Post from `users` LEFT OUTER JOIN ON (User.id=Post.user_id)
                  

                  让它自动为每一列使用别名?或者有没有其他更快/更方便的方法来做到这一点?或者我可以这样做而无需自己定义任何别名吗?

                  And have it automatically use aliases for every column? Or is there any other faster/more convenient way to do this? Or can I do this without having to define any aliases myself?

                  谢谢

                  推荐答案

                  我觉得在这种情况下别名是完美的

                  I think alias is perfect in this case

                  SELECT table1.column AS column1, table2.column AS column2
                  FROM table1
                  LEFT JOIN table2
                  ON table1.column = table2.column
                  

                  为了节省一些打字时间,请使用表格别名:

                  To save you some typing time, use table aliases:

                  SELECT t1.column AS column1, t2.column AS column2
                  FROM table1 AS t1
                  LEFT JOIN table2 AS t2
                  ON t1.column = t2.column
                  

                  这篇关于PHP/MySQL:在无别名的联接查询中获取多个同名列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <small id='yLdoM'></small><noframes id='yLdoM'>

                        <tbody id='yLdoM'></tbody>
                      <tfoot id='yLdoM'></tfoot>

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

                            <bdo id='yLdoM'></bdo><ul id='yLdoM'></ul>