Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:06:52+00:00 2026-06-10T20:06:52+00:00

I have following mysql database tables: cities states countries with members samajs (a group

  • 0

I have following mysql database tables:

cities

states

countries

with

members

samajs (a group of people internationally)

and I want to create a query for my dashboard page which will have following results:

Country   Members Samajs Total   (table header)

Country1    7      5     16      (country row with total members, samajs and total count)

  state1    5      2     7       (state row with total members, samajs and total count)

    city1   3      1     4       (all cities in that state, city row with total members, samajs and total count)

    city2   2      0     2         

  state2    2      1     3

Country2    3      2     5      (country row with total members, samajs and total count)

 ...and vice versa....

Here, members table will have country_id, state_id and city_id as foreign key

samajs table will also have country_id, state_id and city_id as foreign key

Any idea, what will be query for same?

Thanks !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:06:53+00:00Added an answer on June 10, 2026 at 8:06 pm

    I have prepared a query change is according your table fields:

    // for country based 
    SELECT  
    countries.name AS countryName,
    (SELECT
    count('x') FROM members WHERE members.country_id = countries.id) as totalMembers,
    COUNT(samajs.country_id) as totalSamajs
    FROM 
        `countries`
    INNER JOIN samajs ON samajs.country_id = countries.id
    GROUP BY 
        countries.name
    ORDER BY
       totalMembers DESC
    
    // state based result
    
        SELECT  
    states.name AS stateName,
    (SELECT
    count('x') FROM members WHERE members.state_id = states.id) as totalMembers,
    COUNT(samajs.state_id) as totalSamajs
    FROM 
        `states`
    INNER JOIN samajs ON samajs.state_id = states.id
    GROUP BY 
        states.name
    ORDER BY
       totalMembers DESC
    
    //city based result
    
    SELECT  
    cities.name AS cityName,
    (SELECT
    count('x') FROM members WHERE members.city_id = cities.id) as totalMembers,
    COUNT(samajs.city_id) as totalSamajs
    FROM 
        `cities`
    INNER JOIN samajs ON samajs.city_id = cities.id
    GROUP BY 
        cities.name
    ORDER BY
       totalMembers DESC 
    

    and as per cakephp method you can follow this question:
    Order data based on count of related table data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two tables in my mysql database. Table Name: groups id
I have three mysql table from same database Db1. Three tables have following columns.
I have following tables in my MySql database : +----------------------------------------------+ PROJECT +----------------------------------------------| +----------------------------------------------| project_id
Suppose I have the following two tables in my MySQL database: Table 1:: EMP:
I have an e-commerce MySQL database with the following tables: **Product** ---------- productID -
I have two tables in my MySQL database (table1 and table 2). I want
I have the following three tables in a MySQL database in order to give
I have the following tables setup in a MySQL database: Client: id name Session:
I have the following tables in a MySQL 5.x database: Users: id county_id state_id
I have a MySQL database containing the following tables: Table: Professor Attributes: ID, ProfessorName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.