多表join查询

最近做项目,需要查询连接多个表后与一个表左连接查询,在网上查了很多的资料,没有找到解决的办法,郁闷了我好几天,今天终于试出来了。高兴了一把。
SQL语句操作方式一:
select distinct a.id, c.first_name as owner_name,f.stuname,f.stusexname,f.cardno,f.orgincardno,f.classname,f.state,f.createtime
e.classzone_id,b.school_id from people b,profiles c ,roles d,schools s,
classzone_memberships e,classzones g,users a LEFT JOIN t_consume_card t on t.stuid= a.id
where a.id = b.owner_id and b.id = c.person_id
and b.id = d.person_id and b.id = e.person_id and g.id=e.classzone_id
and s.id=b.school_id and s.id=246 and d.name='student';
左连接的两表必须放在left join的前后,后面可以跟where条件,不能用and而是where。
SQL语句操作方式二:
SELECT * from business_handle as f RIGHT JOIN (select distinct a.id,c.first_name as owner_name,e.classzone_id,b.school_id from users a,people b,profiles c ,roles d,
classzone_memberships e,classzones g where a.id = b.owner_id and b.id = c.person_id and b.id = d.person_id and b.id = e.person_id and g.id=e.classzone_id) as h and f.owner_id = h.id where条件

猜你喜欢

转载自superpeng1988.iteye.com/blog/2161331