14

我有这四个表:

PRODUCTS
---------
PRODUCT_ID
PRODUCT_TITLE
(other fields)

COLORS
---------
COLOR_ID
COLOR_NAME

MATERIALS
---------
MATERIAL_ID
MATERIAL_NAME

IMAGES
---------
IMAGE_ID
BIG
MED
SMALL
THUMB

SIZE
---------
SIZE_ID
SIZE_NAME

并且:

PRODUCT_COLOR
---------
PRODUCT_ID
COLOR_ID

PRODUCT_MATERIAL
---------
PRODUCT_ID
MATERIAL_ID

PRODUCT_SIZE
---------
PRODUCT_ID
SIZE_ID

PRODUCT_IMAGE
---------
PRODUCT_ID
IMAGE_ID
COLOR_ID (can be null)
MATERIAL_ID (can be null)

所有产品都可以有不同的颜色和/或材料。例如,我可以拥有一种产品,该产品具有一种或多种材料选项,但没有关联颜色,反之亦然。输出应该是这样的:

-----------------------------------------------------------------------------
| PRODUCT_ID | PRODUCT_NAME  | COLOR_ID | MATERIAL_ID | IMAGE_ID | SIZE_ID |
-----------------------------------------------------------------------------
| 1          | T-SHIRT       | 1        | null        | 1        | 1        |
| 1          | T-SHIRT       | 1        | null        | 1        | 2        |
| 1          | T-SHIRT       | 1        | null        | 1        | 3        |
| 1          | T-SHIRT       | 1        | null        | 1        | 4        |
| 2          | JEANS         | null     | 1           | 2        | 1        |
| 2          | JEANS         | null     | 1           | 2        | 2        |
| 2          | JEANS         | null     | 1           | 2        | 3        |
| 2          | JEANS         | null     | 1           | 2        | 4        |
| 2          | JEANS         | null     | 1           | 2        | 5        |
| 3          | T-SHIRT VNECK | 2        | 2           | 3        | 1        |
| 3          | T-SHIRT VNECK | 2        | 2           | 3        | 2        |
| 3          | T-SHIRT VNECK | 3        | 2           | 4        | 1        |
| 3          | T-SHIRT VNECK | 3        | 2           | 4        | 2        |
| 3          | T-SHIRT VNECK | 4        | 3           | 5        | 1        |
| 3          | T-SHIRT VNECK | 4        | 3           | 5        | 2        |
-----------------------------------------------------------------------------

我尝试了以下语句,但它返回 0 行:

SELECT PRODUCTS.PRODUCT_ID, PRODUCTS.PRODUCT_TITLE, COLORS.COLOR_ID, MATERIALS.MATERIAL_ID, IMAGES.IMAGE_ID, SIZE.SIZE_ID from PRODUCTS
    INNER JOIN PRODUCT_COLOR ON (PRODUCTS.PRODUCT_ID = PRODUCT_COLOR.PRODUCT_ID)
    INNER JOIN COLORS ON (COLORS.COLOR_ID = PRODUCT_COLOR.COLOR_ID)
    INNER JOIN PRODUCT_MATERIAL ON (PRODUCTS.PRODUCT_ID = PRODUCT_MATERIAL.PRODUCT_ID)
    INNER JOIN MATERIALS ON (MATERIALS.MATERIAL_ID = PRODUCT_MATERIAL.MATERIAL_ID)
    INNER JOIN PRODUCT_IMAGE ON (PRODUCTS.PRODUCT_ID = PRODUCT_IMAGE.PRODUCT_ID)
    INNER JOIN IMAGES ON (IMAGES.IMAGE_ID = PRODUCT_IMAGE.IMAGE_ID)
    INNER JOIN PRODUCT_SIZE ON (PRODUCTS.PRODUCT_ID = PRODUCT_SIZE.PRODUCT_ID)
    INNER JOIN SIZE ON (SIZE.SIZE_ID = PRODUCT_SIZE.SIZE_ID)
    ORDER BY PRODUCTS.id_PRODUCT;

有任何想法吗?

4

3 回答 3

21

你可以这样做:

select p.product_id,
  p.product_name,
  c.color_id,
  m.material_id,
  i.image_id,
  s.size_id
from products p
left join product_color pc
  on p.product_id = pc.product_id
left join colors c
  on pc.color_id = c.colorid
left join product_material pm
  on p.product_id = pm.product_id
left join materials m
  on pm.material_id = m.material_id
left join product_image pi
  on p.product_id = pi.product_id
left join images i
  on pi.image_id = i.image_id
  or c.color_id = i.color_id
  or m.material_id = i.material_id
left join product_size ps
  on p.product_id = ps.product_id
left join size s
  on ps.size_id = s.size_id

我会建议你审查JOINs。在线连接有一个很好的可视化解释,可以帮助您编写这些查询。

于 2012-08-27T17:56:01.337 回答
3

好吧,您需要学习如何构建连接,而我通常这样做的方式是选择一个表并连接下一个和下一个和下一个,直到我得到我想要的结果。

select product_id, product_name
  from products

接下来我加入我需要的第一个,所以我继续说

select p.product_id, p.product_name, pc.color_id
  from products p
  join product_color pc on (pc.product_id = p.product_id)

在加入时,重要的是要弄清楚我是否可能没有什么可加入的,我仍然想看到这条线。所以我宁愿使用左连接

     select p.product_id, p.product_name, pc.color_id
       from products p
  left join product_color pc on (pc.product_id = p.product_id)

这样您就可以添加要加入的每个表。顺便一提。这是作业吗?

于 2012-08-27T17:57:58.360 回答
0

如果您只需要 ID,请保持简单

select p.product_id,
  p.product_name,
  pc.color_id,
  pm.material_id,
  pi.image_id,
  ps.size_id
from products p, 
PRODUCT_COLOR pc, 
product_material pm, 
PRODUCT_SIZE ps, 
PRODUCT_IMAGE pi
where 
p.product_id = pc.product_id(+)
and p.product_id = pm.product_id(+)
and p.product_id = ps.product_id(+)
and p.product_id = pi.product_id(+);
于 2012-08-27T18:01:10.473 回答