@Dimitar,
即使@rhavendc 是正确的,这个问题也应该放在数据库管理员中,我会尽我所知按照相应的顺序回答你的问题。
- 我不知道如何存储场地/工作室地址和地理位置。 [...]
您可以通过网络搜索轻松找到地理位置。以MyGeoPosition 为例。
我希望能够执行类似的搜索
您可以轻松做到这一点。有几种方法可以做到这一点,每种方法都需要对您的 ERD 设计进行一些调整。使用我在下面附加的示例,您可以运行查询以列出所有具有 address_id 后跟城市 ID 的场所。黄色实体是我添加以确保完整性的实体。
例如:
-- venue.name is using the "[table].[field]" format to help
-- the engine recognize where the field is coming from.
-- This is useful if you are pulling the fields of the
-- same name from different tables.
select venue.name, city.name
from venue join
address using (address_id) join
city using (city_id);
注意:您不必包含 city_name。我只是把它扔在那里,所以你可以试一试,看看所有匹配它的场地。
如果您想在附近执行此操作,则必须通过在 ADDRESS 表中添加 neighbor_id 来调整我给您的 ERD。我附上了下面的例子,你还必须添加neighbor_id 从那里,你可以运行这样的查询:
使用此 ERD:
-- Remember the format from the previously mentioned code.
select venue.name, neighborhood.name
from venue join
address using (address_id) join
neighborhood using (neighbor_id);
您可以使用Haversine's Formula根据经度和纬度计算英里、公里等的数量。
- 每个班级都应该有一个时间表,目前我不知道如何设计它。例如:从上午 9 点到上午 10 点的动感单车课、Mo、We、Fr。我希望能够进行如下查询:
- 告诉我场地,在 Mo 上有旋转课程
- 或向我展示旋转、拳击等类别中的所有课程
- 甚至可以带我看看提供动感单车课程的场所
这可以很容易地从我在此处附加的任何一个 ERD 中得出。在CLASS 表中,我添加了一个名为parent_class_id 的字段,它从同一个表中获取class_id。这使用递归,我知道这有点让人头疼。这种递归将允许分配了父类的类显示这些类也在不同的时间提供。
你可以通过这样做得到这个结果:
-- Remember the format from the previously mentioned code.
select class1.name, class1.class_id, class2.class_id
from class as class1,
class as class2
where class1.parent_class_id = class2.class_id;
这可能是一个棘手的问题...如果您想知道哪些场地提供动感单车课程,其中动感单车是课程的一部分或课程名称,而不是类别,这很简单。
试试这个...
-- Remember the format from the previously mentioned code.
select venue_id
from venue join
class using (venue_id)
where class_name = 'spinning';
注意:请记住,大多数 SQL 语言在搜索文字时都区分大小写。您可以尝试使用where UPPER(class_name) = 'SPINNING'。
如果类名中可能包含“spinning”以外的词,请改用:where UPPER(class_name) like '%SPINNING%'。
如果您想知道哪些类提供旋转类,其中旋转是一个类别,那就是棘手的问题。我相信您必须为此使用子查询。
试试这个:
-- Remember the format from the previously mentioned code.
select class_id
from class join
class_category using (class_id)
where cat_id = (select cat_id
from category
where name = 'spinning');
再次强调,SQL 引擎在文字搜索方面通常很敏感。确保您的案例使用正确的大写或小写。
- 我应该在这里创建一个额外的表计划吗?或者只是创建某种创建时间表的视图?如果是一张额外的桌子,我应该如何描述一周中每一天的开始、结束?
是和不是。你可以,但如果你能理解数据库系统中的递归,你就不必了。
希望这会有所帮助。 :)