sql自关联查询

时间:2022-01-01 13:32:51

1、子查询关联

select t.*,

(select parent_name from table where id=t.parent_id) from table t where ...


例如:thinkphp分页自关联查询

$Model = new \Think\Model(); 
$sql="select t.*, (select name from table where id = t.parentId) parent_name fromtable t order by id desc limit ".$Page->firstRow . ",$Page->listRows";
 $list = $Model->query($sql);


2、左连接查询

select * from table t1 left join table t2 on t1.parent_id=t2.id where ...