随机选择的子查询选择中的sql语法错误

时间:2014-05-27 21:27:24

标签: php mysql sql

select op.id, op.nome,op.cognome,
   (select tp.inizio
    from turni_preconf as tp
    where tp.tot_ore = op.ore_giornaliere
    order by rand()
    limit 1
  ) as randomin,
addtime(randomin,op.ore_giornaliere_time) as rnout

from operatori as op

错误是:'字段列表'中的未知列'randomin'我对ex查询中的子查询有点怀疑但是我已经尝试过并且不能正常工作

3 个答案:

答案 0 :(得分:0)

您不能在相同的"等级"中使用字段别名。查询,例如

select somefield AS foo, otherfield = foo AS bar
                 ^^^^^^--create alias
                                      ^^^^--use alias, not ok

答案 1 :(得分:0)

UNTESTED: 更新的加入错误我没有合适的标准。

...也许

select op.id, op.nome,op.cognome, addtime(randomin,op.ore_giornaliere_time) as rnout,
b.randomin
from operatori as op
INNER JOIN 
   (select tot_ore, tp.inizio as randomin
    from turni_preconf as tp
    order by rand()
    limit 1
  ) b on b.tot_ore = op.ore_giornaliere

答案 2 :(得分:0)

将子查询移动到FROM子句...

select op.id, op.nome,op.cognome,
  r.val as randomin,
  addtime(r.val, op.ore_giornaliere_time) as rnout
from operatori as op,
(select tp.inizio as val
 from turni_preconf as tp, operatori as op2
 where tp.tot_ore = op2.ore_giornaliere
 order by rand()
 limit 1
) as r