我在MySQL上的以下嵌套查询中尝试的任何内容都得到了相同的语法错误:
SELECT count(*) FROM TABLE_A AS A
WHERE A.COND = '2K' AND A.B_ID = : SELECT B.B_ID
FROM TABLE_B AS B
WHERE B.NAME = 'XYZ';
请注意,TABLE_B上的NAME列条目是唯一的,并且对于查询逻辑不需要连接。
如何修复语法??
NaïveQ - 自本机SQL以来已经很久了。
TIA。
答案 0 :(得分:2)
你需要在子查询周围做一些事情:
SELECT count(*)
FROM TABLE_A AS A
WHERE A.COND = '2K' AND
A.B_ID = (SELECT B.B_ID
FROM TABLE_B AS B
WHERE B.NAME = 'XYZ');
如果它可以返回多个值,那么您需要in
而不是=
:
SELECT count(*)
FROM TABLE_A AS A
WHERE A.COND = '2K' AND
A.B_ID IN (SELECT B.B_ID
FROM TABLE_B AS B
WHERE B.NAME = 'XYZ');
答案 1 :(得分:0)
您可以避免使用显式子查询,而是使用显式连接:
SELECT COUNT(*) AS COUNT
FROM TABLE_A AS A
JOIN TABLE_B AS B ON A.B_ID = B.B_ID
WHERE A.COND = '2K'
AND B.NAME = 'XYZ';
如果您想要子查询,那么Gordon Linoff的answer就可以了。