我有这个SQL查询:
SELECT
HALLS_REGISTRATION.ID,
(
CAST(
(SELECT
HALLS_REGISTRATION.MONEY_RECEIVED_NUMERIC
FROM
HALLS_REGISTRATION
WHERE
HALLS_REGISTRATION.ID = 2)
AS DECIMAL
)
+
(SELECT
SUM(
CAST(
HALLS_REGISTRATION_PAID.PAID_MONEY
AS DECIMAL
)
)
FROM
HALLS_REGISTRATION_PAID
WHERE
HALLS_REGISTRATION_PAID.REGISTRATION_ID = HALLS_REGISTRATION.ID
)
)
FROM HALLS_REGISTRATION;
这很好用,但这不是我想要的。我想要的是用外部选择返回的2
替换第10行的HALLS_REGISTRATION.ID
,所以当我尝试这种方法时,
SELECT
HALLS_REGISTRATION.ID,
(
CAST(
(SELECT
HALLS_REGISTRATION.MONEY_RECEIVED_NUMERIC
FROM
HALLS_REGISTRATION
WHERE
HALLS_REGISTRATION.ID = ROOT.HALLS_REGISTRATION.ID)
AS DECIMAL
)
+
(SELECT
SUM(
CAST(
HALLS_REGISTRATION_PAID.PAID_MONEY
AS DECIMAL
)
)
FROM
HALLS_REGISTRATION_PAID
WHERE
HALLS_REGISTRATION_PAID.REGISTRATION_ID = HALLS_REGISTRATION.ID
)
)
FROM HALLS_REGISTRATION AS ROOT;
DataGrip将Root视为未解析的符号,我在执行时遇到此错误
[42S22] [42122]未找到“HALLS_REGISTRATION.ID”栏; SQL 语句:
我希望查询与H2,MySQL或两者兼容。
答案 0 :(得分:0)
此:
SELECT
HALLS_REGISTRATION.ID,
(
CAST(
(SELECT
HALLS_REGISTRATION.MONEY_RECEIVED_NUMERIC
FROM
HALLS_REGISTRATION, HALLS_REGISTRATION_PAID
WHERE
WHERE
HALLS_REGISTRATION_PAID.REGISTRATION_ID = HALLS_REGISTRATION.ID)
AS DECIMAL
)
+
(SELECT
SUM(
CAST(
HALLS_REGISTRATION_PAID.PAID_MONEY
AS DECIMAL
)
)
FROM
HALLS_REGISTRATION_PAID
WHERE
HALLS_REGISTRATION_PAID.REGISTRATION_ID = HALLS_REGISTRATION.ID
)
)
FROM HALLS_REGISTRATION;
可能会做你想做的事。
答案 1 :(得分:0)
错误消息不是x
,而是第二个子ROOT
。
SELECT
我不确定,您需要所有这些子查询...
尝试这样的事情:
SELECT
ROOT.ID,
(
CAST(
(SELECT
HALLS_REGISTRATION.MONEY_RECEIVED_NUMERIC
FROM
HALLS_REGISTRATION
WHERE
HALLS_REGISTRATION.ID = ROOT.ID)
AS DECIMAL
)
+
(SELECT
SUM(
CAST(
PAID_MONEY
AS DECIMAL
)
)
FROM
HALLS_REGISTRATION_PAID
WHERE
REGISTRATION_ID = ROOT.ID
)
)
FROM HALLS_REGISTRATION AS ROOT;