E
E
exaller2014-01-16 01:20:22
PHP
exaller, 2014-01-16 01:20:22

SQL query COUNT?

Given:
users:
id | name
articles:
id | user_id | title | name
Required:
users:
id | name | articles_count
where articles_count is the number of news from one user.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2014-01-16
@exaller

You can do this , but it's better to make a separate field / table and recalculate when adding / deleting.

A
Artyom Zubkov, 2014-01-16
@artzub

select a.id, a.name, count(b.id) from users a
left join articles b on b.user_id = a.id
group by a.id, a.name

here is how it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question