2012-05-04 3 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 poll_answersテーブルでSELECT * FROM調査. vote_types WHERE ID =1 を投票方式のテキストにインデックスされている私は投票の種類が何であるかのインデックスを保ちます質問と質問に割り当てられた質問は、poll_referendumテーブル の適切な国民投票に割り当てられます。適切な質問と調査に対応する投票タイプを取得するには

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

関連する問題