表A中一条记录的两个字段都对应于表B的同一个字段 如何查询?SQL, thinkphp[5]
表 A=approval_order, B=admin, 表A中technician_username, salesman_username 都是id号,中文名保存在admin表的nickname字段
ThinkPHP代码:
$row = db('approval_order')->alias('ao') ->field('a1.nickname as technician,a2.nickname as salesman,ao.*') ->join('admin a1','ao.technician_username=a1.username') ->join('admin a2','ao.salesman_username=a2.username') ->where('ao.id','=',$id) ->find();
SQL代码:(表名懒得改了,谷歌找到的)
select t1.team_name as team1, t2.team_name as team2, t.team_1, t.team_2 from trades t inner join teams t1 on t1.id = t.team_1 inner join teams t2 on t2.id = t.team_2;