Query to list the amount(Count) of records for a Foreign key

Asked

Viewed 35 times

1

I’m trying to create a query that lists how many times a Foreign key has appeared from another table but I’m not getting it.

Aqui está a tabela 1 - pedido

Aqui está a tabela 2 - restaurante

I wanted to count how many times each customer appears in the order, after several attempts to select with Count and Inner Join did not achieve the expected result.

  • But only with what you’ve shown you can register more than one restaurant per order (assuming that each order and restaurant are the primary keys to your respective tables)?

1 answer

2


If you’re wondering how many orders were placed by each restaurant, you can use one GROUP BY to tell:

SELECT
  idrestaurantepedido,
  count(distinct idpedido) as qtd_pedidos
FROM pedidos
GROUP BY idrestaurantepedido

Browser other questions tagged

You are not signed in. Login or sign up in order to post.