标签:
左联结
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";
标签:
原文地址:http://www.cnblogs.com/xiaoliu520/p/5111418.html