左联结
select a.*,b.name as department_name from
(select * from hr_position where id = {$id})
as a left join hr_department as b on a.department_id = b.id
/**
* 内联结* 1:自连接* select 字段1,字段2 from 表1,表2 where 表1.字段 = 表2.字段(AS是可以省略)*/$sql = "select A.cat_name,B.art_title from bg_category AS A,bg_article AS B where A.cat_id = B.cat_id";//内联结// select 字段1,字段2 from 表1 inner join 表2 on 表1.字段 = 表2.字段$sql1 = "select A.cat_name,B.art_title from bg_category A INNER JOIN bg_article B ON A.cat_id = B.cat_id";/**
* 左联结* select 字段1,字段2 from 表1 left join 表2 on 表1.字段 = 表2.字段* 遍历所有的左边表,右表没有的是NULL*/$sql2 = "select A.cat_name,B.art_title from bg_category A left join bg_article B on A.cat_id = B.cat_id";/**
* 右联结* select 字段1,字段2 from 表1 right join 表2 on 表1.字段 = 表2.字段* 遍历所有右边表,左边没有的是NULL*/$sql3 = "select A.cat_name,B.art_title from bg_category A right join bg_article B on A.cat_id = B.cat_id";/**
* 全联结* select 字段1,字段2 from 表1 join 表2 on 表1.字段 = 表2.字段*/$sql4 = "select A.cat_name,B.art_title from bg_category A join bg_article B on A.cat_id = B.cat_id";/**
* 联合查询* union*/$sql5 = "select cat_id,cat_name from bg_category UNION select art_id,art_title from bg_article";/**
* 嵌套查询*/$sql6 = "select cat_id,cat_name from bg_category where cat_id IN (select cat_id from bg_article where cat_id = 1)";/**
* 组合查询* group by*/$sql7 = "select cat_id as v,cat_name from bg_category GROUP BY v desc";例子:
-------------------------------------------------
a表 id name b表 id job parent_id 1 张3 1 23 1 2 李四 2 34 2 3 王武 3 34 4 a.id同parent_id 存在关系 -------------------------------------------------- 1) 内连接 select a.*,b.* from a inner join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 2)左连接 select a.*,b.* from a left join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 3 王武 null 3) 右连接 select a.*,b.* from a right join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 null 3 34 4 4) 完全连接 select a.*,b.* from a full join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 null 3 34 4 3 王武 null