SQL 关系,多种方法,相同的结果? Select,发件人,地点或 Select,发件人,加入,地点
SQL relationships, multiple methods, same result? Select, From, Where OR Select, From, Join, Where
我有 2 个 table,一个 questions
table 和一个 answers
table,示例数据如下:
+-----------------------------------+
| Questions |
+----+------------------------------+
| id | title |
+----+------------------------------+
| 1 | What is your favourite game? |
| 2 | What is your favourite food? |
+----+------------------------------+
+-------------------------------------------------+
| Answers |
+----+------------------------------+-------------+
| id | text | question_id |
+----+------------------------------+-------------+
| 1 | The Last Of Us | 1 |
| 2 | PlayerUnknowns Battlegrounds | 1 |
| 3 | Uncharted | 1 |
| 4 | KFC | 2 |
| 5 | Pizza | 2 |
+----+------------------------------+-------------+
创建一个一对多的关系,因为在一个问题中可以有很多答案,我可以执行以下任一操作:
SELECT
id, text
FROM
answers
WHERE
question_id = 1
或者:
SELECT
answers.id, answers.text
FROM
answers
JOIN
questions
ON
answers.question_id = questions.id
WHERE
questions.id = 1
或者:
SELECT
answers.id, answers.text
FROM
questions
JOIN
answers
ON
questions.id = answers.question_id
WHERE
questions.id = 1
所有 return 以下(预期)结果:
+-----------------------------------+
| Results |
+----+------------------------------+
| id | text |
+----+------------------------------+
| 1 | The Last Of Us |
| 2 | PlayerUnknowns Battlegrounds |
| 3 | Uncharted |
+----+------------------------------+
应该避免其中的任何一个吗?有这样做的首选方法吗?只是好奇一般查询关系的注意事项。
只想得到答案,就不要涉及问题table。
仅 select 个答案。
将未使用的 table 添加到您的查询中完全没有意义 -
它使查询更难阅读,因此更难维护,
并且它使数据库更加努力地工作(尽管现代数据库可能只是优化查询中未使用的部分)以获得相同的结果。
如果你想暗示 "questions" 和 "answers" table 之间的关系,那么你可以将 "questions" table 中的 id 列作为主键和
"answers" 中的 question_id 列作为外键
当您需要来自多个 table 的数据(列)时,您可以使用 JOIN
在你的情况下,如果你想包含标题列,那么你可以 JOIN tables
我有 2 个 table,一个 questions
table 和一个 answers
table,示例数据如下:
+-----------------------------------+
| Questions |
+----+------------------------------+
| id | title |
+----+------------------------------+
| 1 | What is your favourite game? |
| 2 | What is your favourite food? |
+----+------------------------------+
+-------------------------------------------------+
| Answers |
+----+------------------------------+-------------+
| id | text | question_id |
+----+------------------------------+-------------+
| 1 | The Last Of Us | 1 |
| 2 | PlayerUnknowns Battlegrounds | 1 |
| 3 | Uncharted | 1 |
| 4 | KFC | 2 |
| 5 | Pizza | 2 |
+----+------------------------------+-------------+
创建一个一对多的关系,因为在一个问题中可以有很多答案,我可以执行以下任一操作:
SELECT
id, text
FROM
answers
WHERE
question_id = 1
或者:
SELECT
answers.id, answers.text
FROM
answers
JOIN
questions
ON
answers.question_id = questions.id
WHERE
questions.id = 1
或者:
SELECT
answers.id, answers.text
FROM
questions
JOIN
answers
ON
questions.id = answers.question_id
WHERE
questions.id = 1
所有 return 以下(预期)结果:
+-----------------------------------+
| Results |
+----+------------------------------+
| id | text |
+----+------------------------------+
| 1 | The Last Of Us |
| 2 | PlayerUnknowns Battlegrounds |
| 3 | Uncharted |
+----+------------------------------+
应该避免其中的任何一个吗?有这样做的首选方法吗?只是好奇一般查询关系的注意事项。
只想得到答案,就不要涉及问题table。 仅 select 个答案。
将未使用的 table 添加到您的查询中完全没有意义 - 它使查询更难阅读,因此更难维护, 并且它使数据库更加努力地工作(尽管现代数据库可能只是优化查询中未使用的部分)以获得相同的结果。
如果你想暗示 "questions" 和 "answers" table 之间的关系,那么你可以将 "questions" table 中的 id 列作为主键和 "answers" 中的 question_id 列作为外键 当您需要来自多个 table 的数据(列)时,您可以使用 JOIN 在你的情况下,如果你想包含标题列,那么你可以 JOIN tables