2012-06-19 3 views
0

나는이 다음 표와 많은 행을 받고 :많은 IDS

Action_set 테이블에 id_action_p1/2/3Action 테이블의 id_action에 FKS 있습니다
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 

.

Action_set에있는 각 행의 ID에 대해 Action에 해당 행을 가져와야합니다.

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

이 결과로 저를 제공해야합니다 :

예를 들어, Action_set의 첫 번째 행을 얻을 수 있습니다

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

내가 MySQL의에 멍청한 놈, 그래서 아무 생각이 어떻게 할 수 없습니다 :(여기 내 테이블 (이 id_lap 무시)

: 편집

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 

가 어떻게이 최적화 할 수있다 :

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

안녕 얘들 아, 나는 예상대로 정확하게 반환이 솔루션을 발견?

+0

키 정의 방법은 무엇입니까? 당신은 테이블 SQL을 추가 할 수 있습니까? – Jeremy

+0

테이블 Action_set이 이상하게 보입니다. 왜 외래 키가있는 세 행이 있습니까? 1 차 용은 – nischayn22

+0

입니다. @Jeremy : updated. – Fabricio

답변