Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danimohamadnejad's avatar

How to query multiple tables in one go?

Hi. I want to get total number of users (for past month) and total_number of posts (for 2 months ago). How can I use one Mysql query to grap above information?

0 likes
10 replies
Sinnbeck's avatar

@danimohamadnejad it might be possible, but it would be harder to understand/debug. And even if it was faster, it wouldnt be by alot (it might even be slower)

1 like
danimohamadnejad's avatar
Level 2

Thank you all. Following is almost what I need:

select (select count(*) from users) as users_count, (select count(*) from posts) as posts_count;
Sinnbeck's avatar

@danimohamadnejad that query isn't limited to 2 months. I also assumed you wanted to write it in laravel, not directly in mysql? And be sure to test if it is actually faster than just doing 2 queries, and that the results are the same

1 like
danimohamadnejad's avatar

@Sinnbeck Thank you I will check speed. Criteria must be applied to both sub queries and I am using Laravel DB facade to do that

Please or to participate in this conversation.