1

I have two independent queries as:

SELECT COUNT(*) FROM USER;

and

SELECT COUNT(*) FROM POSTS;

for these result I have created a mysql script file named as total.sql:

USE matchstix;
SELECT COUNT(*) FROM USER;
SELECT COUNT(*) FROM POSTS;

when I ran this script at mysql shell then getting results as

mysql>source /home/total.sql;

    Database changed
    +-------+
    |COUNT(*) |
    +-------+
    | 94122 |
    +-------+
    1 row in set (0.03 sec)

    +----------+
    | COUNT(*) |
    +----------+
    |    94007 |
    +----------+

the question is here how can i get the result something like:

+----------+------------+
| COUNT(*) | COUNT(*)   |
+----------+-------------
|    94007 |  94122     |
+----------+--------------
1 row in set (0.44 sec)

is it possible if yes then how ??

roeygol
  • 4,908
  • 9
  • 51
  • 88
Greesh Kumar
  • 1,847
  • 3
  • 27
  • 44

2 Answers2

4

You can use a SELECT with sub-queries to show the totals in one row:

SELECT 
  (SELECT COUNT(*) FROM USER) AS count_user,
  (SELECT COUNT(*) FROM POSTS) AS count_post

demo on dbfiddle.uk

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
1

Try to use JOIN statement of SQL query. Basicly, you should write two select statement for two different table, and join them together into one view.

I would suggest you to take a look at this : https://stackoverflow.com/a/23700994/1867076

Community
  • 1
  • 1
Prometheus
  • 1,522
  • 3
  • 23
  • 41