mysql - calculating no of users at each place -


i have table place store details place like

place(id,name,detail,type,pid);//pid parent place id

user(id,name,detail);

user_place(uid,pid);//primary key of user , place table

here place can area,city,state or country.

i want track no of users perticular place

like area want fetch total users of area i.e. 400

again city want fetch total user(that total user of area of city) i.e 50000(for city).

similar state , country.

for calculation above data, place table structure correct? or modification required? how calculate no of users according city,state,country?

assumptions: user in area. area in city, city in country.

total number of users in area arkham:

select count(up.uid)   place p     inner join user_place       on p.id = up.pid   p.type = 'area' , p.name = 'arkham'; 

total number of users in city gotham, has multiple areas.

select count(up.uid)   place p     inner join place p2       on p.pid = p2.id , p.type = 'area' , p2.type = 'city'     inner join user_place       on p.id = up.pid     p2.name = 'gotham' 

there demo of here: http://sqlfiddle.com/#!9/14238/5


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -