错误:
``与''预期但标识符indv发现:indv join com
代码:
select temp.CAND_NAME, count(*)
from
(
indv join com
on indv.OTHER_ID = com.CMTE_ID
join can
on indv.CMTE_ID = can.CAND_PCC) as temp
group by temp.CAND_ID
having temp.CAND_ID = "P00003392" or
temp.CAND_ID = "P60006111" or
temp.CAND_ID = "P60007168" or temp.CAND_ID = "P80001571"
可能出现什么问题?
我在做什么:
我有三张桌子:
indv,com,and can。
我想加入所有三个,并查询can.CAND_NAME以及count(*)
我想如何加入?
2件事:
答案 0 :(得分:2)
括号和别名不正确。我会把它写成:
select CAND_NAME, count(*)
from indv join
com
on indv.OTHER_ID = com.CMTE_ID join
can
on indv.CMTE_ID = can.CAND_PCC
where CAND_ID IN ('P00003392', 'P60006111', 'P60007168, 'P80001571')
group by CAND_ID;
注意:
group by
where
之前过滤,而不是之后使用having
。in
而不是一堆or
表达式。答案 1 :(得分:2)
您可以将其简化为:
SELECT cn.CAND_NAME, COUNT(*)
FROM indv i
INNER JOIN com cm
ON i.OTHER_ID = cm.CMTE_ID
INNER JOIN can cn
ON cn.i.CMTE_ID = cn.CAND_PCC
WHERE
cn.CAND_ID IN ('P00003392', 'P60006111', 'P60007168', 'P80001571')
GROUP BY
cn.CAND_ID, cn.CAND_NAME
OR
条件可以写为IN
。GROUP BY
子句中放置非聚合列。答案 2 :(得分:1)
检查出来
SELECT can.CAND_NAME,COUNT(*)
FROM indv inner join com
on indv.OTHER_ID=com._CMTE_ID
inner join can
on indv.CMTE_ID=com.CAND_PCC
WHERE CAND_ID IN ('P00003392', 'P60006111', 'P60007168, 'P80001571')
group by can.CAND_NAME
用户' IN'而不是' OR' 。这应该按照您的预期工作。