码迷,mamicode.com
首页 > 其他好文 > 详细

夺标连接查询 a->c<-b 和a->b->c两种类型

时间:2016-01-12 01:12:49      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

a->b->c多个表之间相互传递的关系

$sql =

<<<EOF

select a.*,d.exam_name,d.subject_id,d.subject_name from ex_user_exam as a left join

(select b.id,b.name as exam_name,b.subject_id,c.name as subject_name from ex_exam as b left join ex_subject as c on b.subject_id = c.id) as d

on a.exam_id = d.id

EOF;

用外链接方法从底层往上查询

 

a->c<-b 两个表都和另外一个表有关系

$sql =
<<<EOF
(select a.id,a.info,a.subject_id,a.label_id,a.label_name,a.add_time,b.name as subject_name,a.type from ex_question as a left join ex_subject as b on a.subject_id = b.id)
union
(select a.id,a.info,a.subject_id,a.label_id,a.label_name,a.add_time,b.name as subject_name,5 as type from ex_material as a left join ex_subject as b on a.subject_id = b.id)
EOF;

union方法拼接记录数:要求是字段统一

夺标连接查询 a->c<-b 和a->b->c两种类型

标签:

原文地址:http://www.cnblogs.com/xiaoliu520/p/5123040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!