1
Example with 2 tables:
table admin:
columns: | id_admin | email | password |
values: | 1 | ex@ex | 123 |
| 2 | ex2@ex2 | 123 |
table news:
columns: | id_news | title | content | admin_id_admin |
values: | 1 | ex | cont | 1 |
| 2 | ex2 | cont2 | 1 |
| 3 | ex3 | cont3 | 2 |
what I want to do: Select all data from all admins and also the amount of news each has
The return would be something like this:
| id_admin | email | password | count_news |
| 1 | ex@ex | 123 | 2 |
| 2 | ex2@ex2 | 123 | 1 |
Is there any way to accomplish this in a query?
How I thought of the query:
SELECT a.id_admin, a.email, a.password, COUNT(b.id_news) AS count_news FROM admin a, news b WHERE a.id_admin = b.admin_id_admin
But it returns "NULL" for all fields except for "count_news".
if you’re using the
count
in select, you need to group, you are missing agroup by a.id_admin, a.email, a.password
in your query– Ricardo Pontual