2012-05-04 69 views
0

您好我有mysql表這樣的:如何從索引到另一個表格行的行中獲取數據?

.poll_users 
(id int(11) NOT NULL AUTO_INCREMENT 
name VARCHAR(255) NOT NULL 
PRIMARY KEY (id)) 

.poll_referendum 
     (id int(11) NOT NULL AUTO_INCREMENT 
     name varchar(255) DEFAULT NULL 
     PRIMARY KEY (id)) 

.poll_questions 
    id int(11) NOT NULL AUTO_INCREMENT 
    referendum_id int(11) DEFAULT NULL 
    body varchar(255) DEFAULT NULL 
    created_at datetime DEFAULT NULL 
    updated_at datetime DEFAULT NULL 
    PRIMARY KEY (`id`) 
    KEY `referendum_id` (`referendum_id`)) 

.poll_answers 
`id` int(11) NOT NULL AUTO_INCREMENT 
`vote_id` int(11) DEFAULT '0' 
`question_id` int(11) NOT NULL 
`created_at` datetime DEFAULT NULL 
`updated_at` datetime DEFAULT NULL 
PRIMARY KEY (`id`) 
KEY `vote_id` (`vote_id`) 
KEY `question_id` (`question_id`)) 

.poll_voting 
    `id` int(11) NOT NULL AUTO_INCREMENT 
    `question_id` int(11) NOT NULL DEFAULT '0' 
    `answer_id` int(11) NOT NULL DEFAULT '0' 
    `user_id` int(11) NOT NULL DEFAULT '0' 
    `created_at` datetime DEFAULT NULL 
    `updated_at` datetime DEFAULT NULL 
    PRIMARY KEY (`id`) 
    KEY `question_id` (`question_id`) 
    KEY `answer_id` (`answer_id`) 
    KEY `user_id` (`user_id`)) 

.vote_types 
    `id` int(11) NOT NULL AUTO_INCREMENT 
    `type` varchar(255) DEFAULT NULL 
    PRIMARY KEY (`id`)) 

我怎樣才能取票類型是vote_types表和對應正確的問題和調查?

我曾嘗試:SELECT vote_id FROM surveys.poll_answers WHERE question_id = 1; 但是這給了我: enter image description here

和1,2是指標表決類型的文本SELECT * FROM調查. vote_types WHERE ID =1 在poll_answers表我把指標投類型是什麼分配到問題和問題被分配到poll_replandum表 適當的全民投票,所以我如何獲取那些投票類型對應於正確的問題和調查 像

SELECT vote_id indexes from poll_answers table and they index to vote_types where are stored vote types in text and those vote_types corresponds to proper questions that are stored in poll_questions and referendums that are stored in poll_referendum ? 

謝謝你的幫助

回答

0

我不確定。也許你在談論表連接。

SELECT 
      v.* 
     FROM poll_answers AS a 
     INNER JOIN poll_voting AS v ON (v.id=a.vote_id) 
     WHERE a.question_id = 1; 
+0

這不工作.. – takeit

相關問題