0

我有一张桌子:

horse
    ->id
    ->course
    ->date

另一个:

course
    ->title
    ->trackstyle

还有第三个

trackstyle
    ->id
    ->style

课程的样式可以有多种,所以我想我会创建一个字段并保存用逗号分隔的值。

IE:

trackstyle
id:1
style:galloping

id:2
style:flat

course
title:ascot
trackstyle:1,2    (as it's galloping and flat)

title=beverley
trackstyle:1      (as it's just flat)

现在,我很难构建一个查询所有三个表的请求......

这是建立我的数据库的正确方法吗?或者我应该在我的课程表中添加一个新列,我将在其中将 trackstyle 设置为“hard”:

course
title: ascot
trackstyle: galloping, flat

title: beverley
trackstyle: flat
4

2 回答 2

0

不,这不是构建数据库的好方法。相反,您必须执行以下操作

course
    ->title

trackstyle
    ->id
    ->style

和一个连接表

 course_trackstyles
       ->course_id
       ->trackstyle_id

您的查询将类似于以下内容

SELECT c.title, GROUP_CONCAT(t.style) 
FROM course c JOIN course_trackstyles ct ON ct.course_id = c.id
              JOIN trackstyles t ON ct.trackstyle_id = t.id
GROUP BY c.title
于 2012-08-17T10:32:13.287 回答
0

这是一对多的关系,因为一门课程可以有多种曲目风格,因此:

course
title: ascot
trackstyle: 1
title: ascot
trackstyle: 2
title: beverley
trackstyle: 2

trackstyle
id: 1
style: galloping
id: 2
style: flat
于 2012-08-17T10:32:22.637 回答