数据库查询多项选择题

时间:2022-10-03 19:21:02

Hi I am Using MYSQL in which i have created a database Survey with tables

嗨,我正在使用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

I want a query to fetch question once only with its options

我想要一个查询只用其选项获取一次问题

 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

Can anyone recommend for the same?

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

1 个解决方案

#1


0  

If you have q.question,o.option fields then You can try `

如果你有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`

Using sub query-

使用子查询 -

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)

#1


0  

If you have q.question,o.option fields then You can try `

如果你有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`

Using sub query-

使用子查询 -

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)