A
A
AAAA2016-09-17 03:04:52
MySQL
AAAA, 2016-09-17 03:04:52

To learn kol-in records in the connected table?

There are two tables
Table_1
id | title
------------
1 | One
2 | Two
3 | Three
Table_2
table_1_id | user_id
------------------------
1 | 1
1 | 2
2 | 1
When selecting from the first table, I want to see
id | title | count
-----------------
1 | One | 2
2 | Two | 1
3 | Three | 0
How to make a query to get the specified result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WebDev, 2016-09-17
@AlexanderWalker

select table1.title, count(table2.id) as count
from table1 
left join table2 on table1.id = table2.user_id
group by table1.id

More or less like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question