Consult MySql using indexes

2

I have a question that I can not solve with Mysql: How can I do the query in this case to obtain the name of the client and the name of the company? I hope for help since I am very involved with this. Thank you!! José

    
asked by Jose Martinez 05.04.2018 в 17:36
source

3 answers

4
SELECT 
E.Empresa_descrip AS EMPRESA,
C.cliente_nombre AS NOMBRE
FROM Invitacion I
INNER JOIN Clientes C ON I.Id_Cliente = C.cliente_id
INNER JOIN Empresa E ON C.empresa_id = E.Empresa_id
WHERE I.Id_Invitado = 'IdInvitado' AND C.Cliente_nombre = 'NombreCliente'

You must chain with JOIN the tables involved in each jump. Please note that I am linking INVITATION with CLIENTS and CLIENTS with COMPANY.

You can read more here about the use of joins

    
answered by 05.04.2018 в 17:50
1

You can generate with joins what you report as I show in the following example:

Tabla Regiones

IdRegion
Region
----------------------

Tabla Provincias


Idprovincia
Provincia
Idregion(FK)
----------------------


Tabla Comunas

Idcomuna
Comuna
provincia (FK)
-----------------------

    SELECT c.comuna, p.provincia, r.region FROM Comunas c 
INNER JOIN Provincias p (ON c.idprovincia= p.idprovincia)
INNER JOIN Regiones r (ON r.IdRegion = p.IdRegion)

with each Join you add the fields you require from the tables thanks to the aliases. Greetings, I hope it will be useful. Now with the purpose of giving rpta to the tables you mention would be as follows:

SELECT 
   c.cliente_nombre AS NombreCliente,
   e.Empresa_descrip AS DescripcionEmpresa 
FROM Invitacion i
INNER JOIN Clientes c (ON i.Id_Cliente = c.cliente_id)
INNER JOIN empresa e (ON c.empresa_id = e.Empresa_id)
WHERE i.Id_invitado = 'TuIdInvitado';
    
answered by 05.04.2018 в 17:41
1

Well, I did not find another way to upload an image to see the format of the tables, work in Mysql. I hope the idea is clearer. Greetings!

    
answered by 05.04.2018 в 19:45