2012-06-19 88 views
0

我有以下表格:越来越多行与许多IDS

Action_set 
id_action_p1 | id_action_p2 | id_action_p3 | etc. 
1   | 1   | 2 
2   | 3   | 1 
1   | 1   | 1 

Action 
id_action | id_type | value 
1   | 0  | NULL 
2   | 1  | NULL 
3   | 2  | NULL 

id_action_p1/2/3Action_set表是FKS至Actionid_action

对于Action_set中每行的每个id,我需要获取Action中的相应行。

例如,让我们Action_set的第一行:

(id_action_p1 | id_action_p2 | id_action_p3) 
1 | 1 | 2 

必须给我的结果:

(id_type | value) 
0 | NULL 
0 | NULL 
1 | NULL 

我小白与MySQL,所以不知道该怎么办:(

编辑:这里我表(忽略id_lap

CREATE TABLE IF NOT EXISTS `Action` (
    `id_action` int(11) NOT NULL AUTO_INCREMENT, 
    `value` int(11) DEFAULT NULL, 
    `id_type` tinyint(4) NOT NULL, 
    PRIMARY KEY (`id_action`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ; 

INSERT INTO `Action` (`id_action`, `value`, `id_type`) VALUES 
(1, NULL, 0), 
(2, NULL, 1), 
(3, NULL, 2), 
(4, NULL, 3), 
(5, NULL, 4), 
(6, NULL, 5); 


CREATE TABLE IF NOT EXISTS `Action_set` (
    `id_action_set` int(11) NOT NULL AUTO_INCREMENT, 
    `id_lap` int(11) NOT NULL, 
    `id_parent_action_set` int(11) DEFAULT NULL, 
    `id_action_pu` int(11) DEFAULT NULL, 
    `id_action_p1` int(11) DEFAULT NULL, 
    `id_action_p2` int(11) DEFAULT NULL, 
    `id_action_p3` int(11) DEFAULT NULL, 
    `id_action_p4` int(11) DEFAULT NULL, 
    `id_action_p5` int(11) DEFAULT NULL, 
    `id_action_p6` int(11) DEFAULT NULL, 
    `id_action_p7` int(11) DEFAULT NULL, 
    `id_action_p8` int(11) DEFAULT NULL, 
    `id_stage` tinyint(4) NOT NULL, 
    PRIMARY KEY (`id_action_set`), 
    KEY `fk_Action_set_Lap` (`id_lap`), 
    KEY `fk_Action_set_Action_set1` (`id_parent_action_set`), 
    KEY `fk_pu` (`id_action_pu`), 
    KEY `fk_p1` (`id_action_p1`), 
    KEY `fk_p2` (`id_action_p2`), 
    KEY `fk_p3` (`id_action_p3`), 
    KEY `fk_p4` (`id_action_p4`), 
    KEY `fk_p5` (`id_action_p5`), 
    KEY `fk_p6` (`id_action_p6`), 
    KEY `fk_p7` (`id_action_p7`), 
    KEY `fk_p8` (`id_action_p8`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11; 

INSERT INTO `Action_set` (`id_action_set`, `id_lap`, `id_parent_action_set`, `id_action_pu`, `id_action_p1`, `id_action_p2`, `id_action_p3`, `id_action_p4`, `id_action_p5`, `id_action_p6`, `id_action_p7`, `id_action_p8`, `id_stage`) VALUES 
(1, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 2, 2, 2, 0), 
(2, 1, 1, 1, 1, 1, 1, 2, 1, NULL, NULL, NULL, 0), 
(3, 1, 2, NULL, NULL, NULL, NULL, NULL, NULL, 4, 4, 4, 1), 
(4, 1, 3, NULL, NULL, NULL, NULL, 4, NULL, NULL, NULL, NULL, 1), 
(5, 1, 4, NULL, NULL, NULL, NULL, NULL, NULL, 3, 1, 1, 2), 
(6, 1, 5, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, 2), 
(7, 1, 1, 2, 1, 2, 1, 1, 1, NULL, NULL, NULL, 0), 
(8, 1, 7, NULL, NULL, NULL, NULL, NULL, NULL, 4, 4, 4, 1), 
(9, 1, 8, 4, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, 1), 
(10, 1, 9, NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, 1, 2); 

编辑2个

好家伙,我发现这个解决方案,只返回预期:

SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p1 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p2 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p3 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p4 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p5 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p6 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p7 AND id_action_set = 1 
UNION ALL 
SELECT id_type, value FROM Action, Action_set WHERE id_action = id_action_p8 AND id_action_set = 1 

还有就是如何优化呢?

+0

您的密钥是如何定义的?你可以添加创建表SQL吗? – Jeremy

+0

你的表Action_set看起来很奇怪。为什么你有三行外键?没有主要的 – nischayn22

+0

@Jeremy:更新。 – Fabricio

回答

0
select id_type, value from Action where id_action in 
(select id_p1 as id from Action_set limit 0,1 union all 
select id_p2 as id from Action_set limit 0,1 union all 
select id_p3 as id from Action_set limit 0,1) 

我相信有更好的方法来做到这一点,但这也有效。

+0

你的答案是最接近的。检查我的新更新。 – Fabricio

0

不知道如何使用实际的表数据,这给了我你正在寻找的结果。

与定义action_set

SELECT id_type, value 
FROM action 
    LEFT JOIN action_set ON (id_action = id_action_set) 
WHERE id_action_set = 1 

这将有助于更新您原来的职位,以使用实际的表数据字段,以获得更准确的反应。希望这会帮助你朝着正确的方向前进。由于您使用的是FK,因此上面的LEFT JOIN几乎是无用的(假设您在更改时更新了它)。我只需要更多的信息给你正确的查询。除非别人已经明白你想要完成什么......我仍然有点迷路。

**所有action_set是**

SELECT id_type, value 
FROM action 
    LEFT JOIN action_set ON (id_action = id_action_set) 

**所有action_set是与id_action ** 由于id_action = id_action_set

SELECT id_action, id_type, value 
FROM ACTION 
    LEFT JOIN action_set ON (id_action = id_action_set) 

我是在假设id_action = id_action_set 我纠正没有你提供的SQL中的FK,所以我只假设

+0

,这并没有给我预期的结果。对于每个Action_set列的每个id,我需要得到正确的Action行。 – Fabricio

+0

更新了我的帖子 – Jeremy

+0

我刚刚看到你更新了你的帖子。让我看看它,我会更新这个帖子,我可以找出 – Jeremy

0

你可以得到Action结果在单独列像这样:

SELECT 
    a.* 
    b.id_type AS p1_type, b.value AS p1_value, 
    c.id_type AS p2_type, c.value AS p2_value, 
    d.id_type AS p3_type, d.value AS p3_value 
FROM 
    Action_set a 
INNER JOIN 
    Action b ON a.id_action_p1 = b.id_action 
INNER JOIN 
    Action c ON a.id_action_p2 = c.id_action 
INNER JOIN 
    Action d ON a.id_action_p3 = d.id_action 

这将使你的线沿线的一个结果集:

id_p1 | id_p2 | id_p3 | p1_type | p1_value | p2_type | p2_value | p3_type | p3_value 
1  | 1  | 2  | 0  | NULL  | 0  | NULL  | 1  | NULL 
... 
... 

这将是一个理想的解决方案,但如果你需要得到的结果是在行,你可以这样做:

SELECT 
    * 
FROM 
    (
     SELECT aa.*, bb.id_type, bb.value 
     FROM Action_set aa 
     INNER JOIN Action bb ON aa.id_action_p1 = bb.id_action 

     UNION ALL 

     SELECT aa.*, bb.id_type, bb.value 
     FROM Action_set aa 
     INNER JOIN Action bb ON aa.id_action_p2 = bb.id_action 

     UNION ALL 

     SELECT aa.*, bb.id_type, bb.value 
     FROM Action_set aa 
     INNER JOIN Action bb ON aa.id_action_p3 = bb.id_action 
    ) a 
ORDER BY 
    a.id_action_p1, 
    a.id_action_p2, 
    a.id_action_p3, 
    a.id_type 

这会给你一个结果集,如:

id_action_p1 | id_action_p2 | id_action_p3 | id_type | value 
1   | 1   | 2   | 0  | NULL 
1   | 1   | 2   | 0  | NULL 
1   | 1   | 2   | 1  | NULL 
+0

检查我的新更新 – Fabricio