Home > Backend Development > PHP Tutorial > Thinkphp两张数据表left join怎么让相同字段不被覆盖?

Thinkphp两张数据表left join怎么让相同字段不被覆盖?

WBOY
Release: 2016-06-20 12:27:49
Original
1761 people have browsed it

M('User')->join('LEFT JOIN before_conditions on before_conditions.uid=User.id')->where($mapBarCode)->select();

user表和before_conditions表都有id作为主键

现在我要得到user表的id,但查询出来的结果被before_conditions的id覆盖了,该怎么办?


回复讨论(解决方案)

我知道要改字段别名,原生的我知道,但是tp的数据库语法该怎么改

$Model->field('id,nickname as name')->select();

$name = M('User')->join('LEFT JOIN before_conditions on before_conditions.uid=User.id')->where($mapBarCode)->select();
for($i=0;$i      $name_b=M('before_conditions')->where("")->find();
      $name[$i]['b_id'] = $name_b['id'];
}

dump($name);  输出结果user表的id几不会被覆盖

$Model->field('id,nickname as name')->select();


成功了,不过我这两张表除了id有几十个字段,要自己全部在field里加上吗

$name = M('User')->join('LEFT JOIN before_conditions on before_conditions.uid=User.id')->where($mapBarCode)->select();
for($i=0;$i      $name_b=M('before_conditions')->where("")->find();
      $name[$i]['b_id'] = $name_b['id'];
}

dump($name);  输出结果user表的id几不会被覆盖



我就是希望能只连接一次数据库就能取出
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template