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.
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.
Comments