Help Inner Join Mysql

0

Well it happens the following deal to do an inner join to 2 tables by means of its foreign key but it returns empty and there are 2 registers

in serviteca_clientes I have the primary key called cli_identificacion and in serviteca_ventas I have cli_identificacionque is the foreigner

Good to add your foreign key use the following sentence

ALTER TABLE serviteca_sales ADD CONSTRAINT CliVen FOREIGN KEY (cli_identification) REFERENCES serviteca_clientes (cli_identificacion) ON UPDATE CASCADE ON DELETE CASCADE;

SELECT c.cli_identification, c.cli_name FROM serviteca_sales AS b INNER JOIN serviteca_clientes AS c ON (c. cli_identificacion = b. cli_identificacion ) WHERE c. cli_identificacion = b. cli_identificacion

but returns empty and in serviteca_clientes there are 2 records

    
asked by user90058 17.11.2018 в 20:24
source

1 answer

1

Are you sure that in the serviteca_ventas table there are sales associated with those clients? That is the first thing to take into account. But the problem is in the following line:

WHERE c.cli_identificacion = b.cli_identificacion

Just removing that sentence should bring you the records that exist in the table serviteca_ventas . It should be:

 SELECT campos_selecionados FROM serviteca_ventas AS b 
 INNER JOIN serviteca_clientes AS c ON b.cli_identificacion=c.cli_identificacion

When you make a join and link the corresponding fields, it would be equivalent to where you put it. But given the case you do not need this unless you want to filter by some customer ID. For example:

 SELECT campos_selecionados FROM serviteca_ventas AS b 
 INNER JOIN serviteca_clientes AS c ON b.cli_identificacion=c.cli_identificacion WHERE b.cli_identificacion = '123456789'
    
answered by 17.11.2018 / 21:01
source