我需要执行与此处所示完全相同的操作:INNER or LEFT Joining Multiple Table Records Into A Single Row但是它可以在MS Access Query中使用。
以下是情景:
电话表
+----------------+-------------+
| Field | Type |
+----------------+-------------+
| f_id | int(15) |
| f_client_id | int(11) |
| f_phone_type | varchar(50) |
| f_phone_number | varchar(13) |
+----------------+-------------+
客户表
+-----------------------------+--------------+------+-----+
| Field | Type | Null | Key |
+-----------------------------+--------------+------+-----+
| f_id | int(15) | NO | PRI |
| f_first_name | varchar(13) | YES | MUL |
| f_mi | char(1) | YES | |
| f_last_name | varchar(20) | NO | MUL |
+-----------------------------+--------------+------+-----+
通过标准的LEFT或INNER加入,我得到这样的结论:
+------------+------------+--------------+
| name | Phone Type | Phone Number |
+------------+------------+--------------+
| John Smith | Home | 712-555-6987 |
| John Smith | Work | 712-555-1236 |
+------------+------------+--------------+
我需要一个查询,它会为我提供属于给定客户的工作和家庭电话号码
+------------+----------------+--------------+
| Name | Work Number | Home Number |
+------------+----------------+--------------+
| John Smith | 712-555-1236 | 712-555-6987 |
+------------+----------------+--------------+
SQL中的解决方案是
SELECT CONCAT(c.f_first_name, ' ', c.f_last_name) as Client_Name,
wp.f_phone_number as Work_Number,
hp.f_phone_number as Home_Number
FROM clients c
LEFT OUTER JOIN phone hp
ON hp.f_client_id = c.f_id
AND
hp.phone_type = 'home'
LEFT OUTER JOIN phone wp
ON wp.f_client_id = c.f_id
AND
wp.phone_type = 'work'
然而,这并未转换为MS Access,加入失败。通过Access完成同样的事情的最佳方法是什么?
答案 0 :(得分:5)
有两个问题尚未发现。
当连接两个以上的表时,Access'数据库引擎需要括号。通过使用查询设计器来设置连接,这是最简单的方法。查询设计者也会将LEFT JOIN替换为LEFT OUTER JOIN;这两种方式都适用于Access 2003。
这个很容易在查询设计器中构建,但只返回具有home和work数字的客户端的行。我用它作为起点,然后调整了类似于原始的ON子句。
SELECT
c.f_first_name & " " & c.f_last_name AS [Name],
wp.f_phone_number AS [Work Number],
hp.f_phone_number AS [Home Number]
FROM
(Clients AS c
LEFT JOIN Phones AS hp
ON c.f_id = hp.f_client_id)
LEFT JOIN Phones AS wp
ON c.f_id = wp.f_client_id
WHERE
hp.f_phone_type='Home'
AND wp.f_phone_type='Work';
将这些WHERE条件移动到ON表达式(如SQL Server示例中)将返回所有客户端,无论您是否有任何电话号码存在。但是,该方法将需要围绕ON表达式的括号。并且这些JOINS无法在查询设计器中显示。
SELECT
c.f_first_name & " " & c.f_last_name AS [Name],
wp.f_phone_number AS [Work Number],
hp.f_phone_number AS [Home Number]
FROM
(Clients AS c
LEFT JOIN Phones AS hp
ON (c.f_id = hp.f_client_id AND hp.f_phone_type='Home'))
LEFT JOIN Phones AS wp
ON (c.f_id = wp.f_client_id AND wp.f_phone_type='Work');
更新:对于我自己,我更喜欢使用子查询来执行此操作。
SELECT
c.f_first_name & " " & c.f_last_name AS [Name],
wp.f_phone_number AS [Work Number],
hp.f_phone_number AS [Home Number]
FROM
(Clients AS c
LEFT JOIN [
SELECT f_client_id, f_phone_number
FROM Phones
WHERE f_phone_type='Home'
]. AS hp
ON c.f_id = hp.f_client_id)
LEFT JOIN [
SELECT f_client_id, f_phone_number
FROM Phones
WHERE f_phone_type='Work'
]. AS wp
ON c.f_id = wp.f_client_id;
答案 1 :(得分:0)
您还可以使用子查询,例如:
SELECT (firstname & " " & lastname ) AS fullname,
(SELECT f_PhoneNumber FROM tblPhones
WHERE f_clientID = clients.id AND f_PhoneType = "Home")
AS HomeNumber,
(SELECT f_PhoneNumber FROM tblPhones
WHERE f_clientID = clients.id AND f_PhoneType = "Work")
AS WorkNumber
FROM clients