0

所以我基本上有一个看起来像这样的数据库:

card_types
id    description    price
1     Card1           1.00
2     Card2           2.00
3     Card3           3.00

card_occasions
id    occasion
1     birthday
2     graduation
3     thank you
4     other

type_occasions
ID    TypeID   OccasionID
1     1        1
2     1        2
3     1        4
4     2        3

我正在尝试使用WHERE card_type.ID = 1进行内部连接,从而产生类似的输出,但我不知道从哪里开始。

示例输出:

card_occasion.ID   card_occasions.Name
1                  birthday
2                  graduation
4                  other  

任何帮助,将不胜感激。谢谢。

4

4 回答 4

1

您从需要值的表开始。在Join表格之后,您需要建立关系。

SELECT card_occasions.id, card_occasions.occasion
FROM card_occasion co
INNER JOIN type_occasions to ON (to.OccasionID = co.id)
                                               ^ the relation between two table
WHERE ct.ID = 1
于 2013-10-15T07:27:55.267 回答
1
SELECT A.id,A.occasion FROM card_occasions A JOIN type_occasions B ON B.OccasionID= A.id AND B.TypeID=1
于 2013-10-15T07:28:56.650 回答
1

由于 type_occasions 已经拥有 typeid,您不需要加入类型表。

SELECT o.id, o.occassion
FROM card_occasions o
INNER JOIN type_occasions t ON t.occassionid = o.id
WHERE t.typeid = 1
于 2013-10-15T07:25:19.110 回答
1

如果您真的想链接所有三个表,因为我们在这里看不到,您可以使用此方法。这开始从 type_occasion 表链接到适当的“基本数据”表。

Select
  typeo.OccasionID, co.occasion as Name
From type_occasion typeo
  JOIN card_type ct ON (typeo.TypeID=ct.id)
  JOIN card_occasion co ON (typeo.OccasionID=co.id)
Where
  typeo.TypeID=1
  -- ct.id=1
于 2013-10-15T07:32:26.510 回答