MS Access中的多个左连接使用子查询

时间:2017-11-13 19:15:42

标签: sql ms-access

我正在尝试使用多个左连接运行查询。我知道MS Access在连接周围有额外的参数需要(),否则我会收到“Missing Operator”错误,但是我使用的子查询会增加更多的复杂性。我无法弄清楚语法。有人可以帮忙吗?这是我的代码:

import UIKit

// Make a delegate protocol
protocol ViewControllerBDelegate: class {
    func didTapSwitch(isOn: Bool)
}

class ViewControllerB: UIViewController {

    // Make a weak delegate reference in VC B
    weak var delegate: ViewControllerBDelegate?

    var state: Bool?

    // On action trigger delegate method:
    @IBAction func switchTapped(_ sender: UISwitch) {
        delegate?.didTapSwitch(isOn: sender.isOn)
    }

}

class ViewControllerA: UIViewController {

    var stateFromSwitch: Bool?
    // In this VC you are instantiating viewController B
    // ... code ...
    // set delegate: viewControllerB.delegate = self
}

// implement ViewControllerBDelegate
extension ViewControllerA: ViewControllerBDelegate {
    func didTapSwitch(isOn: Bool) {
        stateFromSwitch = isOn
    }
}

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:3)

虽然MS Access'方言需要围绕成对表(可以是嵌套对)的括号,SQL语言作为一般规则(在MS Access之外)要求子查询包含在括号中,表示包含的实体。

具体来说,对于您的特定JOIN子句,您只需要包装前两个派生表并离开外部而不包含任何外围括号。但是,所有子查询都有自己的包裹括号。下面的缩进尝试显示括号配对。

最后,子查询中的ORDER BY是不必要的,因为外部查询排序很重要。

SELECT nPrice.Commodity AS Commodity, nPrice.CodeName AS CodeName, 
       nPrice.[P/N] AS PartNumber, nPrice.Price AS Today, oPrice.Price AS Tomorrow, 
       pPrice.Price AS Next
FROM 
   (
    (SELECT * 
     FROM qryPrice 
     WHERE [MyDate] = #9/1/2017# AND [Type] = 'Net Price' AND [Commodity] = 'commodityX' 
    ) AS nPrice

   LEFT JOIN          
     (SELECT * 
      FROM qryPrice 
      WHERE [MyDate] = #1/1/2018# AND [Type] = 'Net Price' 
     ) AS oPrice
   ON nPrice.[P/N] = oPrice.[P/N]
  )

LEFT JOIN  
   (SELECT *
    FROM qryPrice
    WHERE [MyDate] = #2/1/2018# AND [Type] = 'Net Price' 
   ) AS pPrice

ON oPrice.[P/N] = pPrice.[P/N]

ORDER BY nPrice.[CodeName], pPrice.[CodeName], oPrice.[CodeName], nPrice.[P/N];