I need help with an SQL statement. I have never worked with INNER JOIN and I can't do it correctly.
As you can see in the previous image, what I am looking for is this:
- Save the
id_invitacion
of the tableinvitacion
- Replace
id_invitado
withnombre
from tableinvitado
, that is, instead of saving theid_invitado
, I want to receive the String found in the columnnombre
- Replace
id_evento
with tableevento
, i.e. instead of saving theid_evento
, I want to receive the String found in the columnnombre
To save the data, I have this code:
int i = 0;
do {
registros[i][0] = rs.getInt("id_invitacion");
registros[i][1] = rs.getString("nombreEvento");
registros[i][2] = rs.getString("nombreInvitado");
i++;
} while (rs.next());
So far this is the sentence that I have created but it does not work for me:
sSQL = "SELECT evento.nombre AS nombreEvento FROM evento "
+ ", invitado.nombre AS nombreInvitado FROM invitado "
+ "INNER JOIN invitacion ON evento.id_evento = invitacion.id_invitado "
+"WHERE evento.nombre LIKE '%" + buscar + "%' ORDER BY id_invitacion";
Error I get:
You have an error in your SQL syntax
Any help is welcome.
As you already suspected, the way you do the joins in your SQL query is invalid. Moreover, I don't see that you are returning the column
id_invitacion
. Here is the corrected version (note:inner join
andjoin
are equivalent):Additionally, you should not use a loop
do-while
, because by doing this, you will be trying to read information from registers before executingrs.next()
, which is not legal and will give you an error. Rather, use awhile
normal loop: