我有以下SQL子请求。它表示子查询返回多行。
select MIN(XXXX)
FROM MyDB.ball b2
where b2.XXXX > b.XXXX and b2.LLLL=b.LLLL and b2.MMMM=b.MMMM
and b2.IIII=b.IIII
在全局请求中使用b = b1 = MyDB.ball。
在完整的背景下:
if(b.XXXX!='9999',
(select DATE_FORMAT(Datedepar,'%d/%m/%Y')
from ball b1 where b1.XXXX=(select MIN(XXXX)
FROM MyDB.ball b2 where b2.XXXX > b.XXXX and b2.LLLL=b.LLLL
and b2.MMMM=b.MMMM and b2.IIII=b.IIII)), '')
as myResult,
我已经看过这篇文章,但我没有小组:SQL MIN() returns multiple values?
答案 0 :(得分:0)
好的,我找到了原因。事实上这很愚蠢。我必须在子查询之后添加另一个where子句。这是(丑陋的)结果:
select DATE_FORMAT(Datedepar,'%d/%m/%Y')
from FODEGI.ball b1 where b1.XXXX=(select MIN(XXXX)
FROM FODEGI.ball b2 where b2.XXXX > b.XXXX and b2.LLLL=b.LLLL
and b2.MMMM=b.MMMM and b2.IIII=b.IIII) and b1.LLLL=b.LLLL
and b1.MMMM=b.MMMM and b1.IIII=b.IIII), ''
答案 1 :(得分:0)
问题是外部子查询返回多行,因为多个值与最小值匹配。
为什么不使用order by
和limit
?
select DATE_FORMAT(Datedepar, '%d/%m/%Y')
from ball b1
where b1.XXXX > b.XXXX and
b1.LLLL = b.LLLL and
b1.MMMM = b.MMMM and
b1.IIII = b.IIII
order by b1.XXXX
limit 1;