N
N
Nikolaymac2018-09-04 14:13:49
MySQL
Nikolaymac, 2018-09-04 14:13:49

How to calculate percentage of total mysql records?

Table Addresses (id_address,region,status)
SELECT count(*),addresses.region FROM addresses GROUP BY region - I count the total number of addresses by region as this query.
How to calculate in one request how many percent of these addresses have status = 5?
as a result, something like this
Region ---- 10,000 addresses --- 90% status 5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stimulate, 2018-09-04
@Stimulate

SELECT COUNT(*)/(SELECT COUNT(*) FROM addresses)*100 AS percent FROM addresses WHERE status=5

D
Darkhan Kamaliev, 2018-09-04
@darakanoit

Usually I would pull out the data of the total number of values ​​in the table and then all with status = 5
And in php I would calculate the ratio.
I think nested queries can help you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question