K
K
kostya17042021-02-12 10:12:44
SQL
kostya1704, 2021-02-12 10:12:44

How to count child elements?

There are two tables :
Firms table:
ID Name
1 Sony
2 Panasonic
3 Samsung
-------------------------------------------------- ----------
Table phones:
id Firma_ID Phone
1 1 332-55-56
2 1 332-50-01
3 2 256-39-11
4 3 556-39-11
5 3 456-39- 11
----------------------------------------------
have a request :

$phones_count = DB::table('firms')
            ->leftJoin('phones', 'firms.id', '=', 'phones.firma_id')
            ->count();

How to create a query to get the number of telephone numbers for each company?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitalyChaikin, 2021-02-12
@kostya1704

SELECT Firms.Name, Count(*) 
FROM phones
LEFT JOIN Firms ON Firms.id = phones.Firma_id
GROUP BY Firms.Name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question