从表JOIN中获取两个字段的值 [英] Get the values of two fields from a table JOIN
问题描述
我有一个名为districts
的mysql表,其中存储了所有区及其ID.我还有另一个名为tbl_units
的表,其中保存了office_address
,office_district
,factory_address
,factory_district
之类的单元详细信息.
I have a mysql table called districts
where all the districts and their id's are stored. I have another table called tbl_units
where unit details such as office_address
, office_district
, factory_address
, factory_district
are saved.
现在,如果要通过加入两个表来从其ID中获取区域的名称,我该如何编写查询?因为
Now if want to get the names of the districts from their id's by JOIN ing the two tables, how should I write the query ? Because
SELECT u.*, d.district_name
FROM tbl_unit_details as u,
tbl_districts as d
WHERE u.unit_id = '$unit_id'
AND u.district_id = d.district_id
AND u.factory_district_id = d.district_id
ORDER BY unit_name
仅返回办公室的第一个名称,即地区名称.
returns only the first , i.e. district name of office.
推荐答案
两次将districts表加入units表;每个地区类型一次.使用别名来区分分区表的每个实例.如果愿意,可以包含可选的AS
关键字.
Join the districts table to the units table twice; once for each district type. Use aliases to distinguish each instance of the district table. If you like, you can include the optional AS
keyword.
这篇关于从表JOIN中获取两个字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!