create view Com as
select C.id,
IF(A.agree = null) THEN
0
else A.agree
end if
+
IF(GA.agree = null) THEN
0
else GA.agree
end if
FROM
comments C left join AGREES A on A.id_comment=C.id left join GAGREES GA on GA.id_comment=C.id
此代码有语法错误我该如何解决这个问题?
答案 0 :(得分:5)
= null
并不意味着什么。您必须使用is null
答案 1 :(得分:3)
试试这个:
SELECT C.id,
IFNULL(A.agree, 0) + IFNULL(GA.agre, 0)
FROM comments C
left join AGREES A on A.id_comment=C.id
left join GAGREES GA on GA.id_comment=C.id
答案 2 :(得分:2)
您无法在此类查询中嵌套if
块。如果您需要实现类似逻辑,那么您将使用case
语句。但是,在此案例中,您只需在线使用coalesce()
或isnull()
。
select
c.id
,isnull(a.agree,0) + isnull(ga.agree,0)
from Comments as c
left join Agrees as a
on a.id_comment = c.id
left join GAgrees ga
on ga.id_comment = c.id
......或者(以及MYSQL需要的东西):
select
c.id
,coalesce(a.agree,0) + coalesce(ga.agree,0)
from Comments as c
left join Agrees as a
on a.id_comment = c.id
left join GAgrees ga
on ga.id_comment = c.id
答案 3 :(得分:0)