how to do this query sql

2

code is PK in both

tabela1: cliente
codigo, nome, municipio

tabela2: notafiscal
codigo, numerodanota, cod_cliente

I want to list a count (*) on all notafiscal where cod_client.municipality = 10

How do I run this sql?

    
asked by anonymous 12.07.2017 / 21:45

2 answers

5

You only need to use JOIN to link between the tables as below:

SELECT COUNT(n.codigo)
  FROM cliente c
       INNER JOIN notafiscal n ON n.cod_cliente = c.codigo
 WHERE c.municipio = 10;
    
12.07.2017 / 21:46
5

Just do a join between the tables and specify the municipality in WHERE .

SELECT count(*) FROM notafiscal as n 
INNER JOIN clientes as c ON n.cod_cliente =  c.codigo
WHERE c.municipio = 10
    
12.07.2017 / 21:48