我正在编写需要三列的SQL查询。其中之一是COUNT,它是多个子查询的结果。我在FROM ..行收到错误消息“ SQL命令未正确结束”。我想这是一个语法错误,与上面的COUNT部分有关。我在做错事时可以得到一些帮助吗?
SELECT STUDENTS.NAME AS NAME,
ADDRESS.CITY AS CITY,
COUNT(*) AS MY_COUNT FROM (SELECT MYSTATE FROM SOME_TABLE
WHERE SOME_ATTRIBUTE IN
(SELECT SUBQUERY_2 =
(SUBQUERY_3)))
FROM MYSCHEMA.ADDRESS ADDRESS --Error: SQL command not properly ended
INNER JOIN SCHEMA.STUDENTS STUDENTS
ON ADDRESS.ID=STUDENTS.ID
GROUP BY STUDENTS.NAME, STUDENTS.CITY
答案 0 :(得分:2)
我想您的第一个FROM与您要依赖的表/查询有某种关系。如果是这样,那么这样做是错误的形式。您需要将其转换为标量子查询:
SELECT STUDENTS.NAME AS NAME,
ADDRESS.CITY AS CITY,
(SELECT COUNT(*) FROM (...) WHERE ...) AS MY_COUNT
FROM MYSCHEMA.ADDRESS ADDRESS
...
从您提供给我们的内容中,无法猜测需要放在内部FROM / WHERE内部的内容。这是因为“其中之一是COUNT,它是多个子查询的结果”对于解释要尝试实现(计数)准确的内容来说,这是毫无希望的。因此,如果您需要帮助,请详细说明 HOW “ COUNT是多个子查询的结果”。
答案 1 :(得分:1)
现在尝试此解决方案:
SELECT STUDENTS.NAME AS NAME,ADDRESS.CITY AS CITY,
(SELECT COUNT(*) FROM SOME_TABLE WHERE SOME_ATTRIBUTE IN (SELECT SUBQUERY_2 = (SUBQUERY_3))) AS MY_COUNT
FROM MYSCHEMA.ADDRESS ADDRESS
INNER JOIN SCHEMA.STUDENTS STUDENTS ON ADDRESS.ID=STUDENTS.ID
GROUP BY STUDENTS.NAME, STUDENTS.CITY