select customers with orders in Sequelize ORM

Viewed 39

Suppose I have the traditional database of customers and orders

I need a query that returns only those customers with orders in the Sequelize ORM

In SQL it would be something like

SELECT DISTINCT name FROM Customers c INNER JOIN Orders o ON o.customerId = c.id

But in sequelize, without raw statements, how would it be?

Rafael

1 Answers

use group by

SELECT 
c.name,
o.customerId, 
Count(o.order_id) Number_of_Orders
FROM Customers c 
JOIN Orders o ON o.customerId = c.id
group by c.name,o.customerId,
Related