sql多表分类查询

来源:互联网 发布:淘宝商品资质图片 编辑:程序博客网 时间:2024/06/10 02:47

一般的多表查新用到的就是inner join ,left join,right join之类,根据相同的字段值进行比较然后拼接起来

inner join 是内连接,就是只列出满足条件的数据

left join 是左外连接查询,就是查询出所有左表的数据,然后符合查询条件的列出,不符合查询条件的在后面列为空

right join类似左外连接,不在阐述

需要注意的是你group by了什么,你在select中就要有相应的查询

select sum(USER_AMOUNT) as sum,stat_time ,c.project_name as projectName ,d.customer_name as customerNamefrom reg_user_stat a inner join product b on a.product_id=b.product_id inner join project c on a.project_id=c.project_id inner join customer d on c.customer_id=d.customer_id inner join business e on d.business_id=e.business_id inner join pic f on e.pic_id=f.pic_id inner join team g on f.team_id =g.team_id inner join area h on g.area_id=h.area_id inner join agent i on h.agent_id=i.agent_id where 1=1 group by stat_time ,c.project_name ,d.customer_name order by stat_time ASC


0 0