SQL 1 - 许多不返回所有记录

时间:2015-09-23 19:20:12

标签: mysql sql

这应该是SQL 101,但我遗漏了一些东西。

我有2张桌子。问题和答案(每个问题有4个可能的答案)。 尝试编写一个查询,返回每个问题以及该问题的每个可能答案。这是2个表的样子(截断)。

+---+--------------+
| X | Question     |
+---+--------------+
| 1 | A lesion is  |
| 2 | A linear acc |
| 3 | A patient wi |
| 4 | A small tumo |
+---+--------------+

+---+--------------+-----+
| X | Answer       | Crt |
+---+--------------+-----+
| 1 | 12 MeV       | Yes |
| 1 | 9 MeV        | No  |
| 1 | 6 MeV        | No  |
| 1 | 16 MeV       | No  |
| 2 | 6 MeV        | Yes |
| 2 | 5 MeV        | No  |
| 2 | 12 MeV       | No  |
| 2 | 20 MeV       | No  |
| 3 | Prostate siz | Yes |
| 3 | T1-T2a       | No  |
| 3 | Gleason scor | No  |
| 3 | PSA <10      | No  |
| 4 | contains few | Yes |
| 4 | contains a h | No  |
| 4 | is easier to | No  |
| 4 | is more easi | No  |
+---+--------------+-----+

这就是我想通过键入X

来获得的
+---+--------------+--------------+-----+
| X | Question     | Answer       | Crt |
+---+--------------+--------------+-----+
| 1 | A lesion is  | 12 MeV       | Yes |
| 1 | A lesion is  | 9 MeV        | No  |
| 1 | A lesion is  | 6 MeV        | No  |
| 1 | A lesion is  | 16 MeV       | No  |
| 2 | A linear acc | 6 MeV        | Yes |
| 2 | A linear acc | 5 MeV        | No  |
| 2 | A linear acc | 12 MeV       | No  |
| 2 | A linear acc | 20 MeV       | No  |
| 3 | A patient wi | Prostate siz | Yes |
| 3 | A patient wi | T1-T2a       | No  |
| 3 | A patient wi | Gleason scor | No  |
| 3 | A patient wi | PSA <10      | No  |
| 4 | A small tumo | contains few | Yes |
| 4 | A small tumo | contains a h | No  |
| 4 | A small tumo | is easier to | No  |
| 4 | A small tumo | is more easi | No  |
+---+--------------+--------------+-----+

这在Access中工作正常 但是在MySQL(5.6)中我得到了

+---+--------------+--------------+-----+
| X | Question     | Answer       | Crt |
+---+--------------+--------------+-----+
| 1 | A lesion is  | 12 MeV       | Yes |
| 2 | A linear acc | 6 MeV        | Yes |
| 3 | A patient wi | Prostate siz | Yes |
| 4 | A small tumo | contains few | Yes |
+---+--------------+--------------+-----+

这是我使用的查询。

SELECT Questions.X, Questions.Question, Answers.Answer, Answers.Correct
FROM Questions LEFT JOIN Answers ON Questions.X = Answers.X;

我也试过INNER JOIN,RIGHT JOIN,LEFT OUTER JOIN,&amp; RIGHT OUTER JOIN,都有相同的结果。

我一直在使用各种数据库这么久,这似乎应该有效。我不知道Access和MySQL之间会有什么区别可以解释这一点。

拜托,我做错了什么?

1 个答案:

答案 0 :(得分:2)

对于您向我们展示的表格和数据your query works。问题不在于您的查询,而是您未向我们展示的其他内容。

调试时间。

确定您的表格中是否包含您认为他们所做的数据?

检查的简单方法:SELECT * FROM questionsSELECT * FROM answers

确定您是否在正确的数据库上运行查询?

也许您正在检查SQL shell中的表但是通过程序运行查询?请务必使用相同的界面执行这两项操作。

确定您正在运行您认为自己正在运行的查询?

您已经发布了一次损坏的查询。也许你没有按照自己的想法运行。

您是否在shell中输入了该查询?或者它是通过某些程序创建和执行的?如果是后者,请在shell中测试它并从程序中打印出完整的查询。您还可以检查MySQL查询日志。

确定您的架构是否符合您的想法?

运行DESCRIBE questionsDESCRIBE answers,仔细检查表格定义是否有任何不愉快。