Answer the question
In order to leave comments, you need to log in
What are the ways to count the number of elements in a linked table?
Suppose there are two tables: buyers (customer) and orders (order).
CREATE TABLE `customer` (
id INT,
name VARCHAR(255)
);
CREATE TABLE `order` (
id INT,
customer_id INT
);
SELECT customer.name,
(SELECT COUNT(*)
FROM `order`
WHERE `order`.customer_id = customer.id) AS order_count
FROM customer;
Answer the question
In order to leave comments, you need to log in
SELECT customer.name, COUNT(`order`.id)
FROM customer
INNER JOIN `order` ON customer.id = `order`.customer_id
GROUP BY customer.name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question