我有以下查询(在我看来)比它需要的时间太长并且包含可以合并的语句。麻烦的是,我不知道怎么做:)。
我有什么办法可以改善这一点,让它“更好”吗?
SELECT `Date`, `Time`,
(SELECT `Buyer Name`
FROM [Potential Buyer]
WHERE `Buyer No` = [Viewing Appointment].`Buyer No`) AS 'Buyer Name',
(SELECT `Name`
FROM [Seller]
WHERE `Seller No` = (SELECT `Seller No`
FROM [Sale Property]
WHERE `Property Code` = [Viewing Appointment].`Property Code`)) AS 'Seller Name',
(SELECT `Address`
FROM [Sale Property]
WHERE `Property Code` = [Viewing Appointment].`Property Code`) AS 'Property Address',
(SELECT `Name`
FROM [Staff Member]
WHERE `Staff ID` = [Viewing Appointment].`Staff Appointed`) AS 'Staff Members Name'
FROM [Viewing Appointment]
此外,目前所选字段的顺序是日期,时间,买方名称,卖方名称,物业地址,员工姓名。无论如何我可以改变这个顺序吗?
感谢。
答案 0 :(得分:0)
是的,看看联接
按如下方式重构SQL:
SELECT
[Viewing Appointment].`Date`,
[Viewing Appointment].`Time`,
[Potential Buyer].`Buyer Name`,
Seller.Name,
[Sale Property].Address,
[Staff Member].Name
FROM [Viewing Appointment]
LEFT JOIN [Potential Buyer] ON [Potential Buyer].`Buyer No` = [Viewing Appointment].`Buyer No`
LEFT JOIN [Sale Property].`Property Code` = [Viewing Appointment].`Property Code`
LEFT JOIN Seller ON Seller.`Seller No` = [Sale Property].`Seller No`
LEFT JOIN [Staff Member] ON [Staff Member].`Staff ID` = [Viewing Appointment].`Staff Appointed`
您可以通过以不同的顺序选择字段来更改字段的顺序