Linux 连接表,-规范化字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:21:43  来源:igfitidea点击:

Joining Tables,- Normalizing Fields

sqljoin

提问by Q2Ftb3k

I have two tables. One table is named "Posts", the other is "Threads". Each of these has 3 columns named (id, author, content). The Posts table has a specific (thread) column. The thread column has to correspond to the id in the Threads table. What I'm trying to do is make one query that will select the thread, and all of its posts, and normalize its resulting fields. Here is what I want to generate:

我有两张桌子。一张表名为“Posts”,另一张表名为“Threads”。其中每一个都有 3 个名为(id、作者、内容)的列。Posts 表有一个特定的(线程)列。线程列必须对应于线程表中的 id。我正在尝试做的是进行一个查询,该查询将选择该线程及其所有帖子,并对其结果字段进行规范化。这是我想要生成的内容:

author | content

作者 | 内容



Person| This is the thread's contents
Person| This would be a post.
Person| And another post.

人| 这是线程的内容
人| 这将是一个帖子。
人| 还有一个帖子。

回答by smdrager

"Normalize its resulting fields"? I'm not sure what you mean by that. Also you contradicted yourself saying both have three fields, but that Posts has a fourth field (foreign key to Threads)?

“标准化其结果字段”?我不确定你的意思。您还自相矛盾,说两者都有三个字段,但 Posts 有第四个字段(线程的外键)?

Also you really probably don't want to do that in one query (it would be far from "normalized"

此外,您可能真的不想在一个查询中这样做(这远非“规范化”

SELECT * FROM Threads WHERE id = @id
SELECT * FROM Posts WHERE thread = @threads

Alternatively,

或者,

SELECT * FROM Posts LEFT JOIN Threads ON Posts.thread = Thread.id WHERE Posts.thread = @thread