在MySQL上加入多个复杂的表

时间:2019-02-14 08:50:02

标签: mysql join inner-join sql-tuning

我正在尝试查找所有在“平台”问题上回答“ facebook”的客户(编号1) enter image description here

每个客户都有调查表,我正在寻找在问题“平台”上回答“ facebook”的客户,问题的ID是固定的,例如问题表中的“平台” ID为1,“年龄”为2,依此类推每个调查表都有不同的问题集,例如1号调查表具有问题“平台”,但是答案将通过选择框而不是文本框输入(因此需要answer_option表),而2号调查表可能没有问题平台,并且等等。将从搜索屏幕中获取的参数是问卷ID和回答“平台”问题的文本。

现在这是我到目前为止所拥有的。

SELECT customers.* FROM customers
INNER JOIN questionnaire_answers
ON questionnaire_answers.answerable_id = customers.id
AND questionnaire_answers.answerable_type = 'Customer'
INNER JOIN questionnaires
ON questionnaires.id = questionnaire_answers.questionnaire_id
INNER JOIN questions
ON questions.questionnaire_id = questionnaires.id
INNER JOIN answers
ON answers.question_id = questions.id
INNER JOIN answer_options
ON answer_options.id = answers.answer_option_id
WHERE (questions.id = 1 and answers.answer LIKE '%facebook%');

如果您注意到question.id始终为1,因为我只是在平台中寻找答案,这是屏幕的特殊功能。 我尝试使用1-20行数据来工作,但是如果数据变得像20,000行一样大,它永远不会返回我已经尝试过使用真实数据的记录,那么查询将永远需要取消它。由于问题ID已经是常数,并且问卷ID来自搜索参数,因此我认为也许不需要联接不必要的表,并且我认为放置索引不会有用,因为我具有通配符参数?(不确定) ,但我不知道如何。

示例数据和预期结果

Customer:
 id:1,
 family_name: "smith",
 first_name: "smith",
..
Customer:
 id:2,
 family_name: "david",
 first_name: "david",
..

QuestionnaireAnswer:
  id: 20199,
  questionnaire_id: 4,
  answerable_type: "Customer",
  answerable_id: "1",
QuestionnaireAnswer:
  id: 20200,
  questionnaire_id: 5,
  answerable_type: "Customer",
  answerable_id: "2",

Questionnaire:
  id: 4,
  name: "Survey for Psychology Students",
  group_code: "10",
Questionnaire:
  id: 5,
  name: "Survey for Students",
  group_code: "10",

Question:
  id: 1,
  name: "platform",
  answer_type: "08", //selectbox
  default_text: nil,
  sequence: 1,
  question_valid: true,
  disabled: false,
  questionnaire_id: 5,
  ...
Question:
  id: 2,
  name: "school",
  answer_type: "01", //textbox
  default_text: nil,
  sequence: 2,
  question_valid: true,
  disabled: false,
  questionnaire_id: 5,
  ...

Question:
  id: 3, // I said the the id for platform is fixed but this time this question is textbox inputted so it is different
  name: "platform",
  answer_type: "01", //text
  default_text: nil,
  sequence: 1,
  question_valid: true,
  disabled: false,
  questionnaire_id: 5,
  ...
Question:
  id: 4,
  name: "town",
  answer_type: "01", //textbox
  default_text: nil,
  sequence: 2,
  question_valid: true,
  disabled: false,
  questionnaire_id: 4,
  ...

Answer:
  id: 1,
  question_id: 3,
  answer_option_id: nil,
  answer: "facebook",
  questionnaire_answer_id: 19585,
  created_at: Wed, 13 Feb 2019 21:43:15 JST +09:00,
  updated_at: Wed, 13 Feb 2019 21:43:15 JST +09:00,
  member_id: nil>,

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您要加入几个不必要的表。您无需使用answer_options中的任何内容,因此不需要。并且answers具有指向questionnaire_answersquestions的外键,因此您不需要questionnaires

SELECT customers.* 
FROM customers AS c
INNER JOIN questionnaire_answers AS qa
ON qa.answerable_id = c.id
INNER JOIN answers AS a
ON a.questionnaire_answers_id = qa.id
INNER JOIN questions AS q
ON q.id = a.question_id
WHERE q.id = 1
    AND a.answer LIKE '%facebook%'
    AND qa.answerable_type = 'Customer'