dongxin8392 2015-01-21 08:47
浏览 21
已采纳

按国家搜索场地似乎有问题

Overview: I am making a concept site for events and because events can tour around a country and the world, I wanted to make it easy for the people who are adding the events, because we already have a list of venues, and new venues are being added each day we have set up our database ER with the following tables

country state suburb

The state table links back to the country table using the id of the country, while the suburb table links back to the state.

Now while this is all good and fine, I think I have found an issue, it more about how do I get the php code to know that that country to search for all the venues that are in that country, if I have not store the country or state id in the venue. I have only store the suburb.

I wanted to have less columns in my database, but I am not thinking why not put those two in, this was we will be able to find the venues, which in return will find the events linked to that venue.

I do wonder if their is a better way to do this, and any feedback would be helpful.

To clarify venues table only links to suburb, and suburb links to state and state links to country.

Here is a copy of part of my ER diagram ER Diagram

  • 写回答

1条回答 默认 最新

  • duanbei6427 2015-01-21 09:00
    关注

    4 Tables: Country, State, Suburb, Venue

    • Venue has an FK to Suburb
    • Suburb has an FK to State
    • State has an FK to Country

    Solution 1: The really bad SQL (don't do this, but kinda fun to think out)

    SELECT * FROM venues WHERE suburb_id IN (
        SELECT suburb_id FROM suburb WHERE state_id IN (
            SELECT state_id FROM state WHERE country_id IN (
                SELECT country_id FROM country WHERE name = ?
            )
        )
    )
    

    Solution 2: Create a lookup table like this for venue to location storing all the relevant values, and then you can look everything up via that lookup table

    VENUE_LOCATION
    id (INTEGER), PK, AUTO INCREMENT
    venue_id (INTEGER), FK to venue id
    suburb_id (INTEGER), FK to suburb id
    state_id (INTEGER), FK to state id
    country_id (INTEGER), FK to country id
    

    Only thing to watch out for is that someone might give you invalid values during insertion or modification of the record, so what you can do is make this table READ, INSERT and DELETE only, and to enforce the integrity from the application layer.

    So, make your code only take in the inputs of VENUE and SUBURB, and your back end code can determine the state and country ids from internal lookups VS user input.

    Then your query to retrieve all the venues by country would be:

    SELECT * FROM venues WHERE venue_id IN (
        SELECT venue_id
            FROM venue_location INNER JOIN country
            ON country.id = venue_location.country_id
            WHERE country.name = ?
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值