如何左转加入旧ID

时间:2017-12-09 07:48:09

标签: mysql

我给出了tlb_comments,这个表是tlb_comments表

tlb_comments:

| ID |  post_id    |   user_id     | comments  |  cm_parent  |  
+----+-------------+---------------+-----------+-------------+
| 11 |      18     |  29           |  Nice     | 11          | 
| 12 |      24     |  30           |  Good     | 12          | 

tlb_avatar表是给定的,这个表是tlb_avatar表

tlb_avatar:

| id |  vw_id      |   image                                 |   
+----+-------------+-----------------------------------------+
|  1 |      29     |  1510553363_User_Avatar_2.png           |  
|  2 |      30     |  d968c2fe95629e238b04ee34599c5013.jpg   | 

tlb_viewer鉴于此表是tlb_viewer表

tlb_viewer:

| id |  name         |     
+----+---------------+
|  1 |      john     |  
|  2 |      Quotos   |  

我的左连接查询输出。

这个Out put是完美的工作,但我希望将tbl_comments id添加到每一行。现在它不会调用tbl_comments id

| id  |  post_id|  user_id | comments|  cm_parent|  vw_id | image |
+----+----------+----------+---------+-----------+--------+-------+
|  29 |  18     |   29     |  Nice   |  11       | 29     | 2.png |
|  30 |  24     |   30     |  Good   |  12       | 30     | 2.png |

它的查询输出很好。但是我想要tlb comment Id(tbl_comments id)到每一行。

我解释给定的表格。这里我们想要最后一列tbl_comments id(注释ID)。

这张表是我期待的表。如何获得???????

| id  |  post_id|  user_id | comments|  cm_parent|  vw_id |comment ID  |
+----+----------+----------+---------+-----------+--------+------------+
|  29 |  18     |   29     |  Nice   |  11       | 29     | 11         |
|  30 |  24     |   30     |  Good   |  12       | 30     | 12         |

MYSQLI QUERY: 那个查询是我的mysqli查询:

$qid = $quotos->id;

 $avatar = mysqli_query($conn,"SELECT * FROM tlb_comments LEFT JOIN     tlb_avatar ON tlb_avatar.vw_id = tlb_comments.user_id LEFT JOIN tlb_viewer
                                                        ON tlb_viewer.id = tlb_comments.user_id  WHERE   tlb_comments.post_id = '".$qid."'"  );
                            while($comments = mysqli_fetch_object($avatar)){}

1 个答案:

答案 0 :(得分:0)

如果使用select *,则连接表的列列表的输出顺序与其表的连接顺序相同。如果您希望列的顺序不同,则列出列。

select user_id as id,
    post_id, user_id, comments, cm_parent,
    vw_id, image,
    tlb_comments.ID as 'comment ID'
from tlb_comments
left join tlb_avatar
...