简体   繁体   English

MySQL多个左外连接查询问题涉及3个表

[英]MySQL Multiple Left Outer Join Query Question involving 3 tables

Due to 0 responses, I'm guessing that my LEFT JOIN question got into too much detail about a database that was too esoteric. 由于0回复,我猜我的LEFT JOIN问题涉及太深奥的数据库太详细了。 I've already programmed around the issue, but I'd still like to know how to join in a similar scenario: 我已经围绕这个问题进行了编程,但我仍然想知道如何加入类似的场景:

Assume a basic surrogate key strategy (each table has an id field that just auto-increments), as well as a foreign key to its obvious parent. 假设一个基本的代理键策略(每个表都有一个只是自动递增的id字段),以及它明显的父类的外键。 Words in all caps can be considered tables. 全部大写的单词可以被视为表格。

Say you have a Database containing DOGS. 假设您有一个包含DOGS的数据库。 Example: Wolfie, Winston, Butch, and Benny 例如:Wolfie,Winston,Butch和Benny

Each DOG has FLEAs. 每个DOG都有FLEA。 (for simplicity lets make it so that one flea lives on only one dog and leave this a 1 to many relationship). (为简单起见,我们可以让一只跳蚤只生活在一只狗身上,并留下一对多的关系)。 The fleas have id's as names or whatever, along with what color they are. 跳蚤有名字或其他任何东西,以及它们的颜色

Each FLEA will BITE it's DOG host several times, and that is stored in this database, and recorded daily. 每个FLEA将多次BITE它的DOG主机,并存储在此数据库中,并每天记录。
Fields id(PK), flea id(FK), date, times_bitten. 字段ID(PK),跳蚤ID(FK),日期,times_bitten。

Say you want to get the total number of times each dog was bitten (this is easy) 假设您想获得每条狗被咬的总次数(这很容易)

SELECT Dog.Name, sum(Bite.times_bitten)
FROM Dog, Flea, Bite
WHERE Dog.id = Flea.Dog_id and Bite.id = Flea.Bite_id
GROUP BY Dog.Name

Let's say that you were to add criteria to the "WHERE" clause limiting it to "Brown" fleas, and no "Brown" Fleas ever bit Benny. 让我们说你要为“WHERE”条款添加标准,将其限制为“布朗”跳蚤,而没有“布朗”跳蚤有点像本尼。

SELECT Dog.Name, sum(Bite.times_bitten)
FROM Dog, Flea, Bite
WHERE Dog.id = Flea.Dog_id and Bite.id = Flea.Bite_id and Flea.color = "Brown"
GROUP BY Dog.Name

Benny is not in the result 班尼不在结果中

How would you rewrite the query so that Benny's name would still show up, with either a 0(preferably) or NULL in the sum filed, rather than just having Benny eliminated altogether from the result? 你将如何重写查询,以便Benny的名字仍然显示,在总和中提供0(最好)或NULL,而不是仅仅从结果中完全取消Benny?

This seems like its about multiple left outer joins.. but with multiple tables like this, the documentation that's readily findable doesn't seem to answer a question involving 3 tables with a value filter in the middle of the 3 tables. 这似乎是关于多个左外连接..但是有这样的多个表,很容易找到的文档似乎没有回答涉及3个表的问题,在3个表的中间有一个值过滤器。

Does anyone have advice on how to rewrite this to allow for multiple left outer joins, or have some other method of keeping all of the dog's names in the query even if the sum = 0? 有没有人有关于如何重写这个以允许多个左外连接的建议,或者有一些其他方法在查询中保留所有狗的名字,即使sum = 0?

This should work: 这应该工作:

SELECT Dog.Name, COALESCE(SUM(Bite.times_bitten), 0) AS times_bitten
FROM Dog
LEFT JOIN Flea
  ON Flea.Dog_id = Dog.id
    AND Flea.color = "Brown"
LEFT JOIN Bite
  ON Bite.id = Flea.Bite_id
GROUP BY Dog.Name

By using LEFT JOIN s, you will pull all the Dog records, even those without corresponding Fleas (for which the columns from the join will be NULL). 通过使用LEFT JOIN ,您将获取所有Dog记录,甚至是那些没有相应Fleas的记录(连接中的列将为NULL)。 You can use COALESCE to set times_bitten to 0 if no records are found (otherwise it would be NULL). 如果没有找到记录,您可以使用COALESCEtimes_bitten设置为0(否则它将为NULL)。

You probably also want to group by Dog.id instead of Dog.Name (unless it is impossible for there to be multiple dogs with the same name?) 您可能还希望通过Dog.id而不是Dog.Name (除非不可能有多个具有相同名称的狗?)

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

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