Mysql查询修改

时间:2015-04-03 16:31:57

标签: mysql subquery

我有这个查询,它可以在结果中返回1行,但是我得到的结果超过1行:错误代码:1242。子查询返回超过1行。

即使有多行,我怎样才能返回所有结果?

代码是:

select numbers 
from vista 
where id = (
    select b.id + 3 from (
      select t1.id, t1.numbers t1val, t2.numbers t2val, t3.numbers t3val 
      from vista t1 
      join vista t2 on t1.id = t2.id-1 
      join vista t3 on t1.id = t3.id-2 
      where t1.id = (select max(id) - 2 from vista)
    ) a
    join (
      select t1.id, t1.numbers t1val, t2.numbers t2val, t3.numbers t3val 
      from vista t1 
      join vista t2 on t1.id = t2.id-1 
      join vista t3 on t1.id = t3.id-2 
      where t1.id < (select max(id) - 2 from vista)
    ) b 
  on a.t1val = b.t1val 
  and a.t2val = b.t2val 
  and a.t3val = b.t3val 
  and a.id <> b.id
) 
order by id limit 1;

1 个答案:

答案 0 :(得分:0)

如果您将查询的开头更改为:

select numbers 
from vista
where id in (

结束于:

order by id;

查询将返回所有匹配组后面的数字。

See this example.