我正在运行查询并收到以下PHP错误消息:Warning: mysqli::query(): (21000/1242): Subquery returns more than 1 row
当我将这些查询放入phpMyAdmin时,我收到此消息:#2014 - Commands out of sync; you can't run this command now
PHP版本5.5.18, MySql版本5.6.21
错误查询
select *,
(select stime
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
as standscore,
(select floor(stime / 60)
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
as standminutes,
(select (stime % 60)
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
as standseconds,
(select substring_index(stime, '.', -1)
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
as standhunds,
(select ecomment
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
as ecomm
from standards_type st
where active = 1
order by displayOrder
好查询
select *,
(select stime
from racetype_standards
where eventID = 7 and stypeID=st.id and gender = 2 and division = 1)
as standscore,
(select floor(stime / 60)
from racetype_standards
where eventID = 7 and stypeID=st.id and gender = 2 and division = 1)
as standminutes,
(select (stime % 60)
from racetype_standards
where eventID = 7 and stypeID=st.id and gender = 2 and division = 1)
as standseconds,
(select substring_index(stime, '.', -1)
from racetype_standards
where eventID = 7 and stypeID=st.id and gender = 2 and division = 1)
as standhunds,
(select ecomment
from racetype_standards
where eventID = 7 and stypeID=st.id and gender = 2 and division = 1)
as ecomm
from standards_type st
where active = 1
order by displayOrder
答案 0 :(得分:1)
其中每一项:
(select stime
from racetype_standards
where eventID = 8 and stypeID=st.id and gender = 2 and division = 1)
必须解析为一行 - 对于任何给定的st.id
,不能有两行符合这些条件。如果 两行,则需要在每个内部查询中按st.id
进行分组,并以某种方式汇总stime
(例如,选择max(stime)
)和每个其他子查询都相同。
或者,如果您希望能够为每个racetype_standards行返回多行,那么构建它可能会有很大不同 - 可能通过执行连接。目前这是一个非常难以维护的查询,因为它做了相同的事情5次[基本上] - 如果你修改它只需要那么很多子查询一次(例如,通过使其成为连接)。在我更常用的SQL风格中,我会执行一次子查询,然后引用新创建的列来执行所有其他位,例如(不确定在mysql中是否可行);另外,我只会把时间拉一次,然后将其留给目标文本构建器(即PHP)来构建分钟/秒/诸如此类的东西 - 更少的代码来维护。
但是,无论哪种方式,我都会尝试将其作为连接 - 似乎您最终需要所有各种eventID?只需将桌子拉下来,然后从那里处理它。