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

  • Home
  • SEARCH
  • 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 7528679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:26:55+00:00 2026-05-30T04:26:55+00:00

I did not found a solution to this, so I want to ask you.

  • 0

I did not found a solution to this, so I want to ask you.

The whole thing is that I have 3 tables: Cities, Districts, Institutions.

  • Districts have a relationship with Cities

Now when I want to register an Institution, I have to choose a District from which the Institution belongs, but if that City doesn’t have any district, I have to choose the City itself. So, the question is, how can I bind these tables for the given situation?

P.S. the Districts and Cities must remain bind

  • 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-05-30T04:26:56+00:00Added an answer on May 30, 2026 at 4:26 am

    Have 2 tables, “Institutions” and “Areas”

    Allow “Areas” to link to itself i.e. area_id, parent_area_id

    This way you always link an Institution to an area_id, and then internal logic can deteremine whether that area is considered to be a District or a City.

    So you now have

    institutions (
        id UNSIGNED INT NOT NULL PK AI,
        area_id UNSIGNED INT NOT NULL,
        name VARCHAR NOT NULL
    )
    

    and

    areas (
        id UNSIGNED INT NOT NULL PK AI,
        parent_area_id UNSIGNED INT NOT NULL DEFAULT 0,
        name VARCHAR NOT NULL,
        type ENUM('city','district') NOT NULL DEFAULT 'city' 
    )
    

    The areas.type field is optional but if you want to define them as such then that may be a way to do that within the database (else just assume that if parent_area_id = 0 then it’s a city, else it’s a district)

    This way when selecting the field all you are doing is

    SELECT *
    FROM institutions
    INNER JOIN areas
    ON areas.id = institutions.area_id
    

    You can be 100% certain where the institution area_id links to, there’s no question mark over whether to go to the Districts or Cities table, it’s definitely going to the areas table which in turn treats Districts and Cities in the same way and presents information in a format which your front end may then interpret as city or district. Optionally you could go a step further if you really wanted to

    SELECT
        i.*,
        COALESCE(a_parent.id,a_child.id) AS city_id,
        COALESCE(a_parent.name,a_child.name) AS city_name
    FROM institutions AS i
    INNER JOIN areas AS a_child
    ON a_child.id = i.area_id
    LEFT JOIN areas AS a_parent
    ON a_parent.id = a_child.parent_area_id 
    

    That for example would always return the city name even if the institution was tied to a specific district within a city

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

Sidebar

Related Questions

I know this is a recurrent/classical topic but I did not found anything that
I've been looking around but I have not found a solution for this problem:
I found some threads saying this was doable, but did not find specific instructions
I just recently found myself writing this line of code, which i did not
I did not find an answer to this question. I have a VOIP application.
I have read so many articles about integration and yet i did not found
Good evening. I found a few questions that seemed to ask the same thing
Did not have luck with these examples: Javascript File remove Javascript FSO DeleteFile Method
Did not upgrade anything, nothing should have changed, but I may have a silly
My fellow programmers did not think to add timestamps to every tables of our

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.