0

why this query doesn't work, im trying to compare the field idlic with an existing user that belongs to that idlic.

the returning message from mysql is an unknown column, how do i save the value for used in a comparison.

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' ))
1
  • where i supposed to do that ... Commented Dec 10, 2014 at 15:58

3 Answers 3

1

You cannot use a column alias in the same select or where that it is defined. MySQL has a convenient work-around, where you can use a having clause instead:

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' )
               );
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Gordon, since you're an expert on this topic. Can this (select licencia from cuentas where idcuenta = ht.cuenta) as idlic be considered as bad practice, or is this perfectly normal and is just a preference?
@Alex . . . I prefer a left join in this case. There are some situations where a correlated subquery has advantages, but this is not one of them.
1

Could you try this?

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' )
    )

Comments

0

you can't use what you bring in "as idlic" in where statement. having a query as one of the select field is also not a good idea what if the (select licencia ...) returns more than one result. you need to join these tables and the put the whole conditions in where statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.