简体   繁体   English

将第二个表中的第二个(条件)结果添加到 SQL 查询

[英]Add second (conditional) result from second table to SQL query

I have two tables in a database.我在一个数据库中有两个表。 One stores names/details of users with an index ID;一个使用索引 ID 存储用户的名称/详细信息; the other stores articles they have written, which just keeps the user's ID as a reference (field author ).其他商店他们写的文章,只保留用户的 ID 作为参考(字段author )。 So far so simple.到目前为止如此简单。 I can easily query a list of articles and include in the query a request for the user's name and status:我可以轻松查询文章列表,并在查询中包含对用户姓名和状态的请求:

SELECT a.name, a.status, s.* FROM articles s, author a WHERE s.author=a.id

The problem comes when I occasionally have a second author credit, referenced in field author2 .当我偶尔有第二作者信用时,问题就出现了,在字段author2 中引用。 Up till now I've been doing what I assume is a very inefficient second query when I iterate through the results, just to get the second author's name and status from the table (pseudocode):到目前为止,当我遍历结果时,我一直在做我认为效率非常低的第二个查询,只是为了从表中获取第二个作者的姓名和状态(伪代码):

while ( fetch a row ) {
    if (author2 != 0) {
        query("SELECT name, status FROM author WHERE id=author2") }
    etc. }

While this worked fine in PHP/MySQL (even if clunky), I'm forced to upgrade to PHP7/PDO and I'd like to get the benefits of unbuffered queries, so this nested query won't work.虽然这在 PHP/MySQL 中运行良好(即使笨重),但我不得不升级到 PHP7/PDO,并且我想获得无缓冲查询的好处,所以这个嵌套查询将不起作用。 Obviously one simple solution would be to PDO->fetchALL() the entire results first before iterating all the result rows in a foreach loop and doing these extra queries per row.显然,一个简单的解决方案是先 PDO->fetchALL() 整个结果,然后再在 foreach 循环中迭代所有结果行并每行执行这些额外的查询。

But it would be far more efficient to get that second bit of data somehow incorporated into the main query, pulling from the author table using the second ID ( author2 ) as well as the main ID, so that there are name2 and status2 fields added to each row.但是将第二位数据以某种方式合并到主查询中会更有效,使用第二个 ID ( author2 ) 和主 ID 从作者表中提取,以便将name2status2字段添加到每一行。 I just cannot see how to do it...我只是看不到该怎么做...

It should be noted that while the primary author ID field is ALWAYS non-zero, the author2 field will contain zero if there is no second ID, and there is NO author ID 0 in the author table, so any solution would need to handle an author2 ID of 0 by providing null strings or something in those fields, rather than giving an error.需要注意的是,虽然主要作者 ID 字段总是非零,但如果没有第二个 ID,author2 字段将包含零,并且作者表中没有作者 ID 0,因此任何解决方案都需要处理author2 ID of 0 通过在这些字段中提供空字符串或其他内容,而不是给出错误。 (Or far less elegantly, a dummy author ID 0 with null data could be added to the author table, I suppose.) (或者不那么优雅,我想可以将一个带有空数据的虚拟作者 ID 0 添加到作者表中。)

Can anyone suggest a revised original query that can avoid such secondary queries?任何人都可以建议可以避免此类二次查询的修订原始查询吗?

Never use commas in the FROM clause.切勿FROM子句中使用逗号。 Always use proper, explicit, standard JOIN syntax.始终使用正确、明确、标准的JOIN语法。

For your query, use LEFT JOIN :对于您的查询,请使用LEFT JOIN

SELECT s.*, a1.name, a1.status, a2.name, a2.status
FROM articles s LEFT JOIN
     author a1
     ON s.author = a1.id LEFT JOIN
     author a2
     ON s.author2 = a2.id

Gordon Linoff's answer looks like what you need. Gordon Linoff 的回答看起来像你需要的。

I would have added this as a comment but it is too long of a message...我会将此添加为评论,但它的消息太长了......

I just have a question/comment regarding normalization of the database.我只是有一个关于数据库规范化的问题/评论。 Would there ever be an instance when there is an author3?会不会有一个 author3 的例子? If so then you should probably have an ArticleAuthor table.如果是这样,那么您可能应该有一个 ArticleAuthor 表。 Since you are rebuilding the code anyway this may be an improvement to consider.由于您正在重建代码,因此这可能是一个需要考虑的改进。

I don't know the names and data types of the information you are storing so this is a primitive example of the structure I would suggest.我不知道您存储的信息的名称和数据类型,因此这是我建议的结构的原始示例。

Table Article
ArticleID
ArticleData...

Table Author
AuthorID
AuthorName
AuthorStatus

Table ArticleAuthor
ArticleID
AuthorID

If the Status is dependent on the Author Article combination then AuthorStatus would be moved to ArticleAuthor table like this.如果 Status 依赖于 Author Article 组合,那么 AuthorStatus 将像这样移动到 ArticleAuthor 表。

Table ArticleAuthor
ArticleID
AuthorID
Status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM