How can I get the count of multiple table rows storing in different variables using single query?

Jagaran Shrestha

I have 4 different tables and I want to get the count rows of each tables and get the count(*) on 4 different variables.

The query that I have used is:

 SELECT count(*) as count1 FROM `table1` 
 UNION ALL 
 SELECT count(*) as count2 FROM `table2`
 UNION ALL 
 SELECT count(*) as count3 FROM `table3`
 UNION ALL
 SELECT count(*) as count4 FROM `table4`

The query gives me the count of each rows but all the value gets stored on the same variable name.

 [
  {
    "count1": 16171
  }, 
  {
    "count1": 5928
  }, 
  {
    "count1": 33318
  }, 
  {
    "count1": 5877
  }
 ]

Is there any way i could get the output as;

 [
  {
    "count1": 16171
  }, 
  {
    "count2": 5928
  }, 
  {
    "count3": 33318
  }, 
  {
    "count4": 5877
  }
 ]

Thanks in advance.

Elliott Brossard

UNION ALL unions rows. It sounds like you just want to list each of the counts in the select list. Try this:

SELECT
  (SELECT COUNT(*) FROM `table1`) AS count1,
  (SELECT COUNT(*) FROM `table2`) AS count2,
  (SELECT COUNT(*) FROM `table3`) AS count3,
  (SELECT COUNT(*) FROM `table4`) AS count4
;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I count rows based on multiple different groups?

From Dev

How can I get the row count of a table that is in a different database from where the query is run?

From Dev

How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

From Dev

How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

From Dev

How can I select two different values from another table using only a single query?

From Dev

How can I update multiple rows in a table with SQL query?

From Dev

How to get multiple count in single query

From Dev

How to get multiple count in single query

From Dev

How to get count of single and multiple matches in a single SQL query

From Dev

Get multiple rows in a single query

From Dev

How can I compare multiple rows with other rows from two different table and check if they are equal or not?

From Dev

Using multiple Count in a Single Query

From Dev

How can I send multiple $_GET["x"] variables using htaccess?

From Dev

How to update multiple rows in a table using joins and aggregate functions in a single query

From Dev

How to update multiple rows in a table using joins and aggregate functions in a single query

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

Sybase : How I can get table definition using query?

From Dev

Can I set multiple variables value in single query?

From Dev

Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?

From Dev

Can I get rows count by populated property in mongoose query?

From Dev

How do I return multiple columns and rows in a SQL Count Query?

From Dev

How to get multiple rows from single row of table?

From Java

How to insert multiple rows from a single query using eloquent/fluent

From Dev

How can I count different mysql rows in one database request?

From Dev

i want fetch the multiple data available in single row-column of table using query how to do that?

From Dev

How can I count the numbers of rows that a phalcon query returned?

From Dev

How can I use VBA to get STDEV of the values for different types of objects listed in multiple rows?

From Dev

How can I count number of distinct rows of a table?

Related Related

  1. 1

    How can I count rows based on multiple different groups?

  2. 2

    How can I get the row count of a table that is in a different database from where the query is run?

  3. 3

    How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

  4. 4

    How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

  5. 5

    How can I select two different values from another table using only a single query?

  6. 6

    How can I update multiple rows in a table with SQL query?

  7. 7

    How to get multiple count in single query

  8. 8

    How to get multiple count in single query

  9. 9

    How to get count of single and multiple matches in a single SQL query

  10. 10

    Get multiple rows in a single query

  11. 11

    How can I compare multiple rows with other rows from two different table and check if they are equal or not?

  12. 12

    Using multiple Count in a Single Query

  13. 13

    How can I send multiple $_GET["x"] variables using htaccess?

  14. 14

    How to update multiple rows in a table using joins and aggregate functions in a single query

  15. 15

    How to update multiple rows in a table using joins and aggregate functions in a single query

  16. 16

    How to get count from another table (zero when no data) using single query in mysql?

  17. 17

    How to get count from another table (zero when no data) using single query in mysql?

  18. 18

    Sybase : How I can get table definition using query?

  19. 19

    Can I set multiple variables value in single query?

  20. 20

    Using terminaltables, how can I get all my data in a single table, rather than split across multiple tables?

  21. 21

    Can I get rows count by populated property in mongoose query?

  22. 22

    How do I return multiple columns and rows in a SQL Count Query?

  23. 23

    How to get multiple rows from single row of table?

  24. 24

    How to insert multiple rows from a single query using eloquent/fluent

  25. 25

    How can I count different mysql rows in one database request?

  26. 26

    i want fetch the multiple data available in single row-column of table using query how to do that?

  27. 27

    How can I count the numbers of rows that a phalcon query returned?

  28. 28

    How can I use VBA to get STDEV of the values for different types of objects listed in multiple rows?

  29. 29

    How can I count number of distinct rows of a table?

HotTag

Archive