I have the user id_user=5
who has posts in three different tables
questions (id_post | id_user ...) (7 posts)
marketplace (id_post | id_user ...) (9 posts)
jobs (id_post | id_user ...) (3 posts)
So I want to count, using a single query, the number of posts he has in each table
This is what I have so far...but it doesnt seems to work
(SELECT
COUNT(p.*) as count_questions
FROM $table_questions as p
WHERE p.id_user = :id_user1
)
UNION
(
SELECT
COUNT(m.*) as count_marketplace
FROM $table_marketplace as m
WHERE m.id_user = :id_user2
)
UNION
(SELECT
COUNT(e.*) as count_jobs
FROM $table_jobs as e
WHERE e.id_user = :id_user3
)
what am doing wrong?
I was expecting to retrieve in php like this
$count_questions = $result["count_questions"];
but it doesnt seems to work
question from:https://stackoverflow.com/questions/65617660/count-results-different-tables-single-query-mysql-php