如何从索引到另一个表行的行中获取数据?

时间:2012-05-04 15:31:47

标签: mysql

嗨我有这样的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_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 ? 

感谢您的帮助

1 个答案:

答案 0 :(得分: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;