SQL 运行 很好,但如果用于创建视图,我得到 "Error Code: 1242. Subquery returns more than 1 row"
SQL run fine but if used to create a view I got "Error Code: 1242. Subquery returns more than 1 row"
我 运行 遇到了 MySQL 的一些奇怪问题。我有一个相当长的查询(在下面截断),其中包含子查询:
select
SITEID as ID,
"Predicted" as KIND,
ATTRIBUTE as ATTRIBUTE_ID,
"1" as TYPE,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour where siteid = ID and attribute = ATTRIBUTE_ID and TS = date_format((now() - interval 4 hour), '%Y-%m-%d %H:00:00')) as MINUS4,
if(PREDICTEDRATE is null, 0, PREDICTEDRATE) as CURRENT,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour where siteid = ID and attribute = ATTRIBUTE_ID and TS = date_format((now() + interval 1 hour), '%Y-%m-%d %H:00:00')) as PLUS1,
from
predictionperhour
where
TS = date_format(now(), '%Y-%m-%d %H:00:00')
group by
SITEID,
ATTRIBUTE
如果我 运行 此查询进入 SQL Workbench (不限制结果)我得到了我的 2027 行并且一切正常。
现在,如果我使用完全相同的 select 创建一个视图,然后尝试一个简单的 select * 从那里我得到上面的错误 "Error Code: 1242. Subquery returns more than 1 row".
请问这是怎么发生的?
我试图修改子查询来进行计数,但据我所知,所有结果都是 1 ...
我什至尝试添加一个 LIMIT 1,但尽管在这种情况下我没有遇到任何错误,但我说我没有从子查询中取回预期结果,而是取回了 0.
你能帮我找出问题出在哪里吗?
谢谢
像这样使用 tables (p0, p1, p2) 的别名。如果我们不给它取别名,那么可能会有列 attribute
= ATTRIBUTE_ID
来自同一个 table predictionperhour AS p0
来自外部查询而不是来自内部查询。
select
SITEID as ID,
"Predicted" as KIND,
ATTRIBUTE as ATTRIBUTE_ID,
"1" as TYPE,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour AS p1 where p1.siteid = p0.ID and p1.attribute = p0.ATTRIBUTE_ID and TS = date_format((now() - interval 4 hour), '%Y-%m-%d %H:00:00')) as MINUS4,
if(PREDICTEDRATE is null, 0, PREDICTEDRATE) as CURRENT,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour AS p2 where p2.siteid = p0.ID and p2.attribute = p0.ATTRIBUTE_ID and TS = date_format((now() + interval 1 hour), '%Y-%m-%d %H:00:00')) as PLUS1,
from
predictionperhour AS p0
where
TS = date_format(now(), '%Y-%m-%d %H:00:00')
group by
SITEID,
ATTRIBUTE
我 运行 遇到了 MySQL 的一些奇怪问题。我有一个相当长的查询(在下面截断),其中包含子查询:
select
SITEID as ID,
"Predicted" as KIND,
ATTRIBUTE as ATTRIBUTE_ID,
"1" as TYPE,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour where siteid = ID and attribute = ATTRIBUTE_ID and TS = date_format((now() - interval 4 hour), '%Y-%m-%d %H:00:00')) as MINUS4,
if(PREDICTEDRATE is null, 0, PREDICTEDRATE) as CURRENT,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour where siteid = ID and attribute = ATTRIBUTE_ID and TS = date_format((now() + interval 1 hour), '%Y-%m-%d %H:00:00')) as PLUS1,
from
predictionperhour
where
TS = date_format(now(), '%Y-%m-%d %H:00:00')
group by
SITEID,
ATTRIBUTE
如果我 运行 此查询进入 SQL Workbench (不限制结果)我得到了我的 2027 行并且一切正常。 现在,如果我使用完全相同的 select 创建一个视图,然后尝试一个简单的 select * 从那里我得到上面的错误 "Error Code: 1242. Subquery returns more than 1 row".
请问这是怎么发生的? 我试图修改子查询来进行计数,但据我所知,所有结果都是 1 ... 我什至尝试添加一个 LIMIT 1,但尽管在这种情况下我没有遇到任何错误,但我说我没有从子查询中取回预期结果,而是取回了 0.
你能帮我找出问题出在哪里吗?
谢谢
像这样使用 tables (p0, p1, p2) 的别名。如果我们不给它取别名,那么可能会有列 attribute
= ATTRIBUTE_ID
来自同一个 table predictionperhour AS p0
来自外部查询而不是来自内部查询。
select
SITEID as ID,
"Predicted" as KIND,
ATTRIBUTE as ATTRIBUTE_ID,
"1" as TYPE,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour AS p1 where p1.siteid = p0.ID and p1.attribute = p0.ATTRIBUTE_ID and TS = date_format((now() - interval 4 hour), '%Y-%m-%d %H:00:00')) as MINUS4,
if(PREDICTEDRATE is null, 0, PREDICTEDRATE) as CURRENT,
(select if(PREDICTEDRATE is null, 0, PREDICTEDRATE) from predictionperhour AS p2 where p2.siteid = p0.ID and p2.attribute = p0.ATTRIBUTE_ID and TS = date_format((now() + interval 1 hour), '%Y-%m-%d %H:00:00')) as PLUS1,
from
predictionperhour AS p0
where
TS = date_format(now(), '%Y-%m-%d %H:00:00')
group by
SITEID,
ATTRIBUTE