MySQL MySQL三重连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12222525/
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-31 14:43:57  来源:igfitidea点击:

MySQL triple join

mysql

提问by Biker John

Im having a hard time building the triple join query.

我很难构建三重连接查询。

I have three tables:

我有三张表:

House:

房子:

house ID(key) | Address | personID |

Person:

人:

personID (key) | Name | Address | 

Images:// of house

图片://房子

imageID (key) | personID | url | 

I would like to limit the results to 5.

我想将结果限制为5

I want to query house address-es (main), it's owner name & address, and one picture of the owner.

我想查询房屋地址-es(主要),它的所有者姓名和地址,以及所有者的一张照片。

Note: There are up to 3 images of every person (3 rows in image table), but just one is necessary, doesnt matter which one.

注意:每个人最多有 3 张图像(图像表中的 3 行),但只需要一张,不管是哪一张。

回答by TheHe

SELECT h.Address, p.Name, p.Address as OwnerAddress, i.url FROM house AS h
INNER JOIN person AS p ON p.personID = h.personID
INNER JOIN images AS i ON i.personID = p.personID
GROUP BY h.houseID

should work for you.

应该为你工作。

回答by leonardo_assumpcao

Maybe it'd better for you to use LEFT OUTER JOIN, something like

也许你最好使用 LEFT OUTER JOIN,比如

SELECT
   h.Address  as `h_address`,
   p.Name     as `p_name`,
   p.Address  as `p_address`,
   i.url      as `i_url`
FROM house AS `h`
   LEFT OUTER JOIN person AS `p` ON (p.personID = h.personID)
   LEFT OUTER JOIN images AS `i` ON (p.personID = i.personID)
GROUP BY h.houseID

It would display also houses with no registered images.

它还会显示没有注册图像的房屋。

回答by Sabir Hussain

joining three tables

连接三个表

i.e:

IE:

select i_f.*, i_f_d.* , i_f_c.creator_name 
  from indent_form i_f 
  join indent_form_details i_f_d on i_f.id=i_f_d.ind_id 
  join indent_form_creator i_f_c on i_f.user_id=i_f_c.id

indent_form table 1,
indent_form_details table 2,
indent_form_creator table 3,