三个表联合sql语句,查不出来num,一直为0,真心求教!!

三个表联合sql语句,查不出来num,sql语句:select u.UserName,ISNULL (t.num,0) num,l.* from Logs l left join (select LogID,count(CommentID) num from Comments where CommentID is null group by logId ) t on l.LogID =t.logID left join Users u on l.userID=u.UserID where l.LogStatus=1

mysql join语法 http://www.data.5helpyou.com/article209.html

换这样关联语句呢

select table_a.*.table_b.*,table_c from table_a
left join table_b on table_a.关联字段1 = table_b.关联字段1 and table_a.关联字段2 = table_b.关联字段2
left join table_c on table_a.关联字段 = table_c.关联字段
table_a为主表,table_b,table_c为副表

select u.UserName,ISNULL(t.num,0)num,l.*
from Logs 1
left join Comments c on l.LogID = c.logID where c.CommentID is null
left join Users u on l.userID=u.UserID
where l.LogStatus=1
外关联再加子查询写出来会被说的,不专业

$num_query = mysql_query("select sum(cnt) from(select count(*) as cnt from s1 union all select count(*) as cnt from s2 union all select count(*) as cnt from s3) t");

这样子就出来了。

核心思路是用到哪个就写哪个子查询

最简单的方法就是,拆开局部分析;
先查 logs 表,如果能查出来;再left join 表1;看是否能查出来;
同样再left join 表2;
然后就能找到问题了~~

三表连接度娘都有,一搜一大片,你先得确定你有正确的测试数据,还有就是你检查检查语法什么的

左外连接 右外连接 外连接都可以,你如果查询的麻烦的话可以直接建一个视图,个人建议