# Count number of rows from multiple tables in a single query

**URL:** <https://forum.kirupa.com/t/count-number-of-rows-from-multiple-tables-in-a-single-query/272583>\
**Category:** programming\
**Created:** [October 6, 2008, 7:32pm UTC](https://forum.kirupa.com/t/count-number-of-rows-from-multiple-tables-in-a-single-query/272583 "2008-10-06T19:32:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishalraj9](https://avatars.discourse-cdn.com/v4/letter/v/db5fbb/32.png) [@vishalraj9](https://forum.kirupa.com/u/vishalraj9)\
**Post date:** [October 6, 2008, 7:32pm UTC](https://forum.kirupa.com/t/count-number-of-rows-from-multiple-tables-in-a-single-query/272583/1 "2008-10-06T19:32:13Z")

</div>

Hi all.  
I am trying to count the number of rows from multiple tables in a single query based on criteria. For example, say I have 2 tables, books & bags but based on certain type of a book & certain type of a bag. Now I want to count the number of rows in books table & the number of rows in bags table, all in the same query. I tried some queries like the following:

```php

   $sql = "SELECT count(*) FROM $books AS books_rows, (SELECT count(*) FROM $bags AS bags_rows) WHERE books_rows.book_id = '$book_id' AND bags_rows.bag_id = '$bag_id' AND bags_rows.available = 'y' AND (bags_rows.in_stock = 'y' OR bags_rows.color_available = 'y' OR bags_rows.sold_out = 'n') GROUP BY books_rows.on_id, bags_rows.post_id ";

$result = mysql_query($sql) ;

I tried different ways of query, like:
$rows = mysql_fetch_array($result);
 $sql_data = mysql_fetch_row($result);

```

Nothing really seems to get me the count from the 2 tables in the same query depending on the criteria. Can some one please suggest a solution?

Thank you in advance.
