I need to do a nested query but under one condition. You see, I have two tables.
Tabla 1-> usuarios_administradores
Tabla 2-> usuarios_colaboradores
The table that presents the inconvenience is " agenda_eventos
". Agenda_eventos has a field called " tipo
", if the type is 1, I have to get the mail from the table " usuarios_administradores
", but if "type" = 0, I have to get the email from the table " usuarios_colaboradores
".
The result I need is something like this (All the fields are from the table agenda_events, except the email, that must be obtained according to the type and the id of the user):
idevento, idempresa, idusaurio, tipousuario, titulo, emailusuario
This is my " agenda_ventos
" table
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_empresa | int(11) | YES | | NULL | |
| id_usuario | int(11) | YES | | NULL | |
| titulo | varchar(50) | YES | | NULL | |
| descripcion | varchar(200) | YES | | NULL | |
| allDay | int(1) | YES | | NULL | |
| inicio_fecha | date | YES | | NULL | |
| fin_fecha | date | YES | | NULL | |
| inicio_hora | time | YES | | NULL | |
| fin_hora | time | YES | | NULL | |
| tipo_evento | int(11) | YES | | NULL | |
| tipo_usuario | int(1) | YES | | NULL | |
| recordar | tinyint(4) | YES | | 1 | |
+--------------+--------------+------+-----+---------+----------------+
Following this article I try to do the following, however, the result is an error. : / I do not know if there is any other recommendation for this. I would be infinitely grateful for your guidance:
select agenda.id, agenda.id_empresa, agenda.id_usuario, agenda.tipo_usuario, agenda.titulo,
(select email if(agenda.tipo_usuario=1 && agenda.id_usuario=empresas_usuariosprincipales.id)from empresas_usuariosprincipales)
from agenda_eventos as agenda where recordar = 1