MySQL查询错误未知列

时间:2014-12-10 15:54:14

标签: mysql

为什么此查询无效,我尝试将字段idlic与属于该idlic的现有用户进行比较。

来自mysql的返回消息是一个未知列,如何保存比较中使用的值。

select *, (select licencia from cuentas where idcuenta = ht.cuenta) as idlic
from hojastrabajo ht
where idlic = (select licencia from asociadolicencia where usuario = (select idusuario from usuarios where nombre = 'desarrollador' ))

3 个答案:

答案 0 :(得分:1)

您不能在定义它的同一selectwhere中使用列别名。 MySQL有一个方便的解决方法,您可以使用having子句代替:

select ht.*, (select licencia from cuentas where idcuenta = ht.cuenta) as idlic
from hojastrabajo ht
having idlic = (select licencia
                from asociadolicencia
                where usuario = (select idusuario from usuarios where nombre = 'desarrollador' )
               );

答案 1 :(得分:1)

你可以尝试一下吗?

select ht.*, c.licencia
from hojastrabajo ht
left join cuentas c
on c.idcuenta = ht.cuenta
where c.licencia = 
    (select licencia from asociadolicencia where usuario = 
        (select idusuario from usuarios where nombre = 'desarrollador' )
    )

答案 2 :(得分:0)

你不能使用你带来的东西" as idlic"在哪里声明。将查询作为选择字段之一也不是一个好主意,如果(select licencia ...)返回多个结果。你需要加入这些表并将整个条件放在where语句中。

相关问题