你好,我想问一下我如何根据我的主表properties
表的id从不同的表中选择不同的值:
我的数据库是这样组织的:
propery table:
----------------------------------------------------
id | title | property_type | city | sector | owner |
----------------------------------------------------
1 | title | 1 | 2 | 6 | 12 |
----------------------------------------------------
property_type table:
--------------
id | english |
--------------
1 | name |
--------------
2 | name 2 |
--------------
owner table:
-----------------------------------
id | ownername | phone |
-----------------------------------
12 | Mike | 27836 |
-----------------------------------
所以我的主要选择命令是:'SELECT id, title, property_type, owner FROM properties ORDER BY id'
例如在owner中,我必须能够选择ownername
和phone
两个参数
这是转储不同的ID,但我想修改这个查询,所以它根据主表中的ID从不同的表中选择值。
任何帮助实现这将是感激的。Select应该通过一个查询来实现。
查询的结果应该是:Id(属性表)、english(property_type表)、ownername(所有者表:)、phone(所有者表)。
看起来这就是你想要的查询:
SELECT
P.id
,PT.english
,O.ownername
,O.phone
FROM property P
JOIN property_type PT
ON PT.id = P.property_type
JOIN owner O
ON O.id = P.owner
这里的SQLFiddle
select p.id, p.title, p.property_type, o.ownername, o.phone, pt.english
from property p join property_type pt
on p.property_type = pt.id
join owner o
on o.id = p.owner
你只需要join
表。如果需要,可以添加更多列。