PostgreSQL从3个表中加入数据 [英] PostgreSQL JOIN data from 3 tables

查看:69
本文介绍了PostgreSQL从3个表中加入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PostgreSQL的新手,正在尝试编写查询。我敢肯定,对于知道自己在做什么的人来说,这很容易-我只是不知道! :)

I'm new to PostgreSQL and trying to get a query written. I'm pretty sure it's easy for someone who knows what they are doing - I just don't! :)

基本上我有三个表。首先,我存储有关患者的详细信息。在第二篇中,我存储了对它们每个图像的引用。在第三部分中,我存储了指向图像文件路径的链接。我没有设计数据库,所以我不确定为什么要分隔图像文件表,但是确实如此。

Basically I have three tables. In the first, I store details about patients. In the second, I store a reference to each image of them. In the third, I store the link to the file path for the image. I didn't design the database, so I'm not sure why the image files table is separated, but it is.

我想做的是从第一个表中选择数据,然后从第二个然后是第三个表中加入数据,因此我以&结果中的文件路径。

What I want to be able to do is select data from the first table, joining in data from a second then third table so I end up with the name & file path in the result.

因此基本结构是:

Table 1:
person_id | name

Table 2:
person_id | image_id

Table 3:
image_id | `path filename`

我要执行的操作是在一个查询中,抓住该人的姓名,然后图像路径文件名。

What I want to do is in one query, grab the person's 'name' and the image 'path filename'.

我对需要的联接具有模板样式的答案感到满意。我不需要用实际代码编写它。 (即我认为您可以给我写一个答案,说 SELECT table1.name,table3.pathfilename FROM JOIN ...等等... )。

I'm happy with a "template" style answer with the join I need. I don't need it to be written in actual code. (i.e. I'm thinking you can just write me an answer that says SELECT table1.name, table3.pathfilename FROM JOIN ... etc...).

推荐答案

类似于:

select t1.name, t2.image_id, t3.path
from table1 t1 inner join table2 t2 on t1.person_id = t2.person_id
inner join table3 t3 on t2.image_id=t3.image_id

这篇关于PostgreSQL从3个表中加入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆