关于db2的查询

时间:2012-11-06 08:36:17

标签: mysql database

我有查询,下面的表格中有以下关系

select * from PRODGP_STRUCTURE
select * from PRODGP_STR_LINK
select * from PRODUCT_GROUP
select * from PROD_PGRP_LINK
select * from PRODUCT

关系是......

select pstr.PSTR_NAME, PG.PRGP_NAME, PRD.PROD_NAME
from prodgp_structure pstr
JOIN PRODGP_STR_LINK pgstrlnk
  ON pgstrlnk.PSTR_ID = pstr.PSTR_ID  
JOIN PRODUCT_GROUP pg 
  ON pgstrlnk.PRGP_CHILD_ID = PG.PRGP_ID
JOIN PROD_PGRP_LINK prdprdgp
  ON prdprdgp.PRGP_ID = pg.PRGP_ID
JOIN product prd
  ON prdprdgp.PROD_ID = prd.PROD_ID;

我已经达到了以下查询,

select * from PRODGP_STRUCTURE where pstr_id in 
(select pstr_id from PRODGP_STR_LINK where PRGP_CHILD_ID  in
(select  prgp_id from PRODUCT_GROUP where PRGP_ID in ( select  PRGP_ID from PROD_PGRP_LINK where PROD_ID in ( select PROD_ID from product where PROD_ID=2))))

请让我知道有没有其他方式来撰写此查询.. !!

1 个答案:

答案 0 :(得分:0)

将表连接在一起,然后您只需按PROD_ID

进行过滤
SELECT pstr.PSTR_NAME, PG.PRGP_NAME, PRD.PROD_NAME
FROM prodgp_structure pstr
INNER JOIN PRODGP_STR_LINK pgstrlnk
  ON pgstrlnk.PSTR_ID = pstr.PSTR_ID  
INNER JOIN PRODUCT_GROUP pg 
  ON pgstrlnk.PRGP_CHILD_ID = PG.PRGP_ID
INNER JOIN PROD_PGRP_LINK prdprdgp
  ON prdprdgp.PRGP_ID = pg.PRGP_ID
INNER JOIN product prd
  ON prdprdgp.PROD_ID = prd.PROD_ID
WHERE prd.PROD_ID = 2;