繁体   English   中英

如何将这两个查询结合起来,使结果在一个表中?

[英]How can I combine these two queries such that the results are in one table?

第一个查询:

SELECT integration_department_map.foreign_department_key AS 'Department Code', department.department_name AS 'Department Name'
FROM integration_department_map
JOIN department ON department.department_id = integration_department_map.department_id
WHERE integration_department_map.client_id = '10134';

第二个查询:

SELECT integration_department_map.foreign_department_key AS 'Department Code', location.location_name AS 'Location Name'
FROM integration_department_map
JOIN location ON location.location_id = integration_department_map.location_id
WHERE integration_department_map.client_id = '10134';

它们都分别返回所需的结果,但我想知道是否有一种方法可以将它们写为一个查询?

正如蜜獾评论中所建议的那样。 只需向位置表添加一个join ,例如:

SELECT 
    i.foreign_department_key AS `Department Code`, 
    d.department_name AS `Department Name`, 
    l.location_name AS `Location Name`
FROM integration_department_map i
JOIN department d
ON d.department_id = i.department_id
JOIN location l
ON l.location_id = i.location_id
WHERE i.client_id = 10134

如果它应该是一个int ,则删除id周围的引号。

也许您正在寻找的是 LEFT JOIN 而不是 JOIN(内连接)。 原始 integration_department_map 中有多少行满足 WHERE 子句,它们是同一行吗?

也许这会奏效-

SELECT 
    i.id,
    i.foreign_department_key AS `Department Code`, 
    d.department_name AS `Department Name`, 
    l.location_name AS `Location Name`
FROM integration_department_map i
LEFT JOIN department d ON d.department_id = i.department_id
LEFT JOIN location l   ON l.location_id = i.location_id
WHERE i.client_id = 10134

暂无
暂无

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

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