0

我有一点问题。我有一个这样的数据库布局:

customer
customer_id, name, age, etc...

customer_survey_question
id, category, caption, type

customer_survey_answer
id, customer_id, customer_survey_question_id, answer

我需要像这样提取他们的答案:

name, age, etc..., question 1, question 2, question 3, etc...

现在我可以做一个子查询:

SELECT
  `customer`.*,
  (
    SELECT `answer`
    FROM `customer_survey_answer`
    WHERE `customer_survey_answer`.`customer_id`=`customer`.`customer_id`
    AND `id`=1
  ) AS `question_1`,
  (
    SELECT `answer`
    FROM `customer_survey_answer`
    WHERE `customer_survey_answer`.`customer_id`=`customer`.`customer_id`
    AND `id`=2
  ) AS `question_2`,
  ....

但是有 14 个问题,我需要能够很快地做到这一点,并将问题扩展到 80 多个。最好的方法是什么?

4

4 回答 4

1

您不能通过查询本身来做到这一点。

在这里查看一些示例代码:How to merge data from 2 tables with MySQL

您可以做的是使用INNER JOINorGROUP_CONCAT然后重新格式化脚本中的数据(无论是php还是其他语言)

使用 JOIN

在这种情况下,这可能会导致每行中有大量多余/不相关的数据

SELECT c.*, csa.answer
FROM customers c
INNER JOIN
    customer_survey_answer csa ON csa.`customer_id`=c.`customer_id`
ORDER BY c.customer_id, csa.customer_survey_question_id

使用 GROUP_CONCAT

这将输出一个单元格(csv 样式)作为答案

SELECT c.*, GROUP_CONCAT(csa.answer) as answers
FROM customers c
INNER JOIN
    customer_survey_answer csa ON csa.`customer_id`=c.`customer_id`
GROUP BY c.customer_id

使用循环

您还可以考虑在数据库中查询带有答案的客户列表,然后运行第二个查询(针对每个返回的客户)以获得他们的答案。这可能会导致大量查询。

第一个查询:

SELECT * FROM customers

第二个查询:

SELECT answer
FROM customer_survey_answer
WHERE customer_id = INSERT_CUSTOMER_ID_HERE}
于 2013-09-27T15:34:44.643 回答
0

如果您想在单个查询中生成一个表,您可以group by在一个查询中使用单个查询INNER JOIN,然后收集多个MAX( CASE ... END )表达式中的值。一开始听起来很复杂,但它确实可靠地工作(只需为每个所需的列添加另一行):

SELECT name, age,
MAX(CASE WHEN customer_survey_question_id=1 THEN answer END) a1,
MAX(CASE WHEN customer_survey_question_id=2 THEN answer END) a2,
MAX(CASE WHEN customer_survey_question_id=3 THEN answer END) a3,
MAX(CASE WHEN customer_survey_question_id=4 THEN answer END) a4,
MAX(CASE WHEN customer_survey_question_id=5 THEN answer END) a5,
MAX(CASE WHEN customer_survey_question_id=6 THEN answer END) a6,
MAX(CASE WHEN customer_survey_question_id=7 THEN answer END) a7,
MAX(CASE WHEN customer_survey_question_id=8 THEN answer END) a8,
...
FROM customer c INNER JOIN customer_survey_answer s 
                ON s.customer_id=c.customer_id
于 2013-09-27T15:50:35.737 回答
0

你想旋转数据

SELECT class,GROUP_CONCAT(member)
FROM tbl
GROUP BY class;

是基本情况

http://www.artfulsoftware.com/infotree/queries.php#78

于 2013-09-27T15:17:21.037 回答
0

强制 DB 输出到结果的列是不明智的。

如果您只想对一个客户进行操作,请进行简单的问题查询,然后回答查询以获取所有内容并将其放在数据库之外 - 例如在 PHP 中。

如果您想要客户列表及其答案,请先进行问题查询,然后使用各自的 ID 进行“答案 x 客户”查询,并使用数据库外的散列将它们放在一起。除非您不为输出列表使用分页,否则它运行良好且快速;)。

哈希可以这样工作

// suppose we have from database
// $answer_list(id,customer_id,customer_name,question_id,answer)
// $question_list(id,question)

// hash 
$customer_list = array();
$customer_hash = array();
foreach ($answer_list as $answer)
{
  $customer_id = $answer['customer_id'];
  if (!isset($customer_hash[$customer_id]))
  {
    $customer_list[] = $customer_id;
    $customer_hash[$customer_id]['name'] = $answer['customer_name'];
  }
  $customer_hash[$customer_id]['answer_hash'][$answer['question_id']] = $answer;
}


// output
foreach ($customer_list as $customer_id)
{
  echo $customer_hash[$customer_id]['id'];
  echo $customer_hash[$customer_id]['name'];
  foreach ($question_list as $question)
  {
    echo $question['question'];
    echo $customer_hash[$customer_id]['answer_hash'][$question['id]]['answer'];
  }
}
于 2013-09-27T15:39:25.163 回答