数据库查询多项选择题

时间:2014-09-22 08:40:49

标签: mysql database relational-database mysql-workbench

您好我正在使用MYSQL,我在其中创建了一个带表格的数据库调查

survey_categories(survey_id,survey_name) -- PK survey_id
survey_question(survey_id,question_id,question) -- PK question_id
survey_option(question_id,option_id,options) -- PK option_id

我希望查询仅使用其选项

获取一次问题
 select q.question,o.options 
   from survey_question q, survey_options o, survey_categories s 
  where q.question_id=o.question_id 
     && s.survey_name='purchase' 
     && s.survey_id=q.survey_id

任何人都可以推荐相同的吗?

1 个答案:

答案 0 :(得分:0)

如果你有q.question,那么o.option字段 你可以试试`

select distinct q.question_id,o.options _id,q.question,o.options from survey_question q, survey_options o, survey_categories s 
  where q.question_id=o.question_id 
     and s.survey_name='purchase' 
     and s.survey_id=q.survey_id
     group by q.question_id`

使用子查询 -

select  o.question_id,o.options from  survey_options o where  o.question_id 
IN(select distinct q.question_id from survey_question q, survey_options o, survey_categories s 
  where q.question_id=o.question_id 
     and s.survey_name='purchase' 
     and s.survey_id=q.survey_id)