MySQL Query以某种方式显示信息

时间:2013-12-27 15:58:09

标签: php mysql sql

我很难尝试查询以某种方式显示结果。 我需要的是每个用户在一行中显示所有用户答案,我无法连接所有答案,因为我的程序需要访问 每个答案的结果和我理解的连接 将返回一个字符串。

这是表格的定义方式:

这是存储用户信息的地方。 用户

+------+---------+-------------+
| u_id | email   | name        |
+------+---------+-------------+
|    1 | bob@b   | bob         |
|    2 | r@e.com | sam         |
|    3 | ra@se   | steve       |
|    4 | tv@we   | rob         |
|    5 | tr@sd   | ted         |
|    6 | qw@as   | john        |
+------+---------+-------------+

Questions
+------+---------+-------------+
| q_id | question_name         |
+------+---------+-------------+
|    1 | Age range?            |
|    2 | Do you use Amazon?    |
|    3 | Do you use Facebook?  |
|    4 | Interested in toys?   |
+------+---------+-------------+

这些是用户可以选择的答案。

Option_Choices
+------+---------+-------------+
|oc_id | opt_choice_name       |
+------+---------+-------------+
|    1 | YES                   |
|    2 | NO                    |
|    3 | 18-24 Years           |
|    4 | 25-35 Years           |
|    5 | Very Interested       |
|    6 | Not Interested        |
+------+---------+-------------+

这是一个问题与每个可能的问题相关的地方 回答这个具体问题。

Question_Options
+------+---------+-------------+
|qo_id | q_id    | oc_id       |
+------+---------+-------------+
|    1 | 1       | 3           | // Age range?: 18-24
|    2 | 1       | 4           | // Age range?: 25-35
|    3 | 2       | 1           | // Do you use Amz? Yes
|    4 | 2       | 2           | ...
|    5 | 3       | 1           | ...
|    6 | 3       | 2           | ...
|    7 | 4       | 5           | ...
|    8 | 4       | 6           |
+------+---------+-------------+

这是存储每个用户给出的答案的地方。

Answers
+------+---------+-------------+
| a_id | u_id    | qo_id       |
+------+---------+-------------+
|    1 | 1       | 2           |
|    2 | 1       | 4           |
|    3 | 1       | 6           |
|    4 | 1       | 7           |
|    1 | 2       | 1           |
|    2 | 2       | 3           |
|    3 | 2       | 6           |
|    4 | 2       | 8           |
|    1 | 3       | 2           |
|    2 | 3       | 4           |
|    3 | 3       | 5           |
|    4 | 3       | 8           |
+------+---------+-------------+

我现在需要的是构建一个查询来显示信息 以类似的方式:

+---------+-------------+-------------+-------------+---------------------+
| email   | Age range?  | Use Amazon? | Use FB?     | Interested in toys? |
+---------+-------------+-------------+-------------+---------------------+
| bob@b   | 18-24 Years |    YES      |    NO       | Very Interested     |
| r@e.com | 25-35 Years |    NO       |    YES      | Not Interested      |
| ra@se   | 25-35 Years |    NO       |    YES      | Not Interested      |
| tv@we   | 18-24 Years |    YES      |    YES      | Not Interested      |
| tr@sd   | 18-24 Years |    YES      |    NO       | Not Interested      |
| qw@as   | 25-35 Years |    YES      |    YES      | Very Interested     |
+------+---------+--------------------+-------------+---------------------+

但是只使用JOIN我会得到类似的东西“

+---------+----------------+-----------------+
| email   | question_name  | opt_choice_name |
+---------+----------------+-----------------+
| bob@b   | Age range?     |  18-24 Years    |
| bob@b   | Use Amazon?    |    YES          |
| bob@b   | Use FB?        |    NO           |
| bob@b   | Inter in toys? |Very Interested  |
| r@e.com | Age range?     |  25-35 Years    |
| r@e.com | Use Amazon?    |    NO       |
| r@e.com | Use FB?        |    YES          |
| r@e.com | Inter in toys? | Not Interested  |

   ...         ...               ...
+---------+----------------+-----------------+

我尝试使用带有这样的查询的枢轴:

SELECT u.email,
(CASE q.question_name WHEN q_id = 1 THEN oc.opt_choice_name ELSE 0) AS 'Age_Range',
(CASE q.question_name WHEN q_id = 2 THEN oc.opt_choice_name ELSE 0) AS 'Amazon',
(CASE q.question_name WHEN q_id = 3 THEN oc.opt_choice_name ELSE 0) AS 'FB',
(CASE q.question_name WHEN q_id = 4 THEN oc.opt_choice_name ELSE 0) AS 'Toys'
FROM Users u
INNER JOIN Ansers a ON u.u_id = a.u_id
INNER JOIN Question_Options qo ON a.qo_id = qo.qo_id
INNER JOIN Questions q ON qo.q_id = q.q_id
INNER JOIN Option_Choices oc ON qo.oc_id = oc.oc_id
GROUP BY u.email

但我的结果好坏参半。

任何建议都将不胜感激。

感谢。

我尝试使用MAX

SELECT u.email,
MAX( CASE q.question_name WHEN q.question_id = 1 THEN oc.option_choice_name ELSE 0 END) AS 'AgeRange',
MAX( CASE q.question_name WHEN q.question_id = 2 THEN oc.option_choice_name ELSE 0 END) AS 'Amazon',
MAX( CASE q.question_name WHEN q.question_id = 3 THEN oc.option_choice_name ELSE 0 END) AS 'FB',
MAX( CASE q.question_name WHEN q.question_id = 4 THEN oc.option_choice_name ELSE 0 END) AS 'toys',
FROM Users u
INNER JOIN Answers a ON u.u_id = a.u_id
INNER JOIN Question_Options qo ON a.qo_id = qo.qo_id
INNER JOIN Questions q ON qo.q_id = q.q_id
INNER JOIN Option_Choices oc ON qo.oc_id = oc.oc_id
GROUP BY u.email

但我得到的结果是:

+---------+-------------+-------------+-------------+---------------------+
| email   | Age range?  | Use Amazon? | Use FB?     | Interested in toys? |
+---------+-------------+-------------+-------------+---------------------+
| bob@b   | YES         |    YES      |    NO       |          NO         |
| r@e.com | YES         |    YES      |    YES      | YES                 |
| ra@se   | YES         |    YES      |    YES      | YES                 |
| tv@we   | YES         |    YES      |    YES      | YES                 |
+------+---------+--------------------+-------------+---------------------+

2 个答案:

答案 0 :(得分:1)

虽然你可以用MySQL做到这一点,但它效率不高,因为MySQL索引的行不是列。

  

索引用于快速查找具有特定列值的行。   如果没有索引,MySQL必须从第一行开始然后读取   通过整个表来查找相关的行。越大了   表,这个成本越多。

http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

大多数人会这样做的方法是用JOIN列举所有问题,例如:

+---------+----------------+-----------------+
| email   | question_name  | opt_choice_name |
+---------+----------------+-----------------+
| bob@b   | Age range?     |  18-24 Years    |
| bob@b   | Use Amazon?    |    YES          |
| bob@b   | Use FB?        |    NO           |
| bob@b   | Inter in toys? |Very Interested  |
| r@e.com | Age range?     |  25-35 Years    |
| r@e.com | Use Amazon?    |    NO       |
| r@e.com | Use FB?        |    YES          |
| r@e.com | Inter in toys? | Not Interested  |

   ...         ...               ...
+---------+----------------+-----------------+

然后在你的PHP中你会做类似的事情(免责声明:未经测试的代码):

$i = 0;
echo "<table><tr><th>Age range?</th><th>Use Amazon?</th><th>Use Facebook?</th><th>Interested in Toys?</th></tr><tr>";
foreach ($results as $column) {
    echo "<td>".$column."</td>"; 
    if ($i != 0 && $i % 4) // Just arbitrarily chose 4, but it would be however many columns you have
        echo "</tr><tr>";

    $i++;
}
echo "</tr></table>";

答案 1 :(得分:0)

解决此问题的另一种方法是分别为每个问题创建临时表,最后创建一个mysql查询以一次性水平获取所有数据。

http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm