SQL查询3个表,其中2个表要调出所有字段,另1个表要调出对应userid的累计记分,其中累计积分有判断条件
代码如下:selectt3.*,t4.*from(selectt1.*,ifnull(t2.num,0)jffrom(select*fromA)t1leftjoin(se...
代码如下:
select t3.*,t4.* from (select t1.*,ifnull(t2.num,0) jf from (select * from A) t1 left join (select *,sum(jifen) num from Bgroup by userid) t2 on t1.userid=t2.userid) t3, (select * from C) t4 where t3.userid=t4.userid
以上代码可在表B中调出表A、表C对应userid所有的累计记分。
问:如果只是调出表B中字段check值为1的 累计记分,应该把这个“条件”加在哪里,而又不影响表A、表C的查询? 展开
select t3.*,t4.* from (select t1.*,ifnull(t2.num,0) jf from (select * from A) t1 left join (select *,sum(jifen) num from Bgroup by userid) t2 on t1.userid=t2.userid) t3, (select * from C) t4 where t3.userid=t4.userid
以上代码可在表B中调出表A、表C对应userid所有的累计记分。
问:如果只是调出表B中字段check值为1的 累计记分,应该把这个“条件”加在哪里,而又不影响表A、表C的查询? 展开
1个回答
展开全部
select t3.*,t4.* from (select t1.*,ifnull(t2.num,0) jf from (select * from A) t1 left join (select *,sum(jifen) num from B where check=1 group by userid) t2 on t1.userid=t2.userid) t3, (select * from C) t4 where t3.userid=t4.userid
更多追问追答
追问
这样不行,提示错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'check=1 group by
userid) t2' at line 1
追答
check是关键字吧
select t3.*,t4.* from (select t1.*,ifnull(t2.num,0) jf from (select * from A) t1 left join (select *,sum(jifen) num from B where `check`=1 group by userid) t2 on t1.userid=t2.userid) t3, (select * from C) t4 where t3.userid=t4.userid
加两个点,上边数字键1左边的那个点,不要用错了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询