0

所以我有一个疑问;一个像这样的模拟FULL OUTER JOIN

SELECT * FROM SupplierInfo
LEFT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
UNION
SELECT * FROM SupplierInfo
RIGHT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId;

然而,这拉出了一个相当大的选择,我只需要 2 - 3 个字段:SupplierPartNoMajorEquipmentNo. 我尝试过这样的事情:

SELECT SupplierPartNo, MajorEquipmentNo FROM(
  SELECT * FROM SupplierInfo
  LEFT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
  UNION
  SELECT * FROM SupplierInfo
  RIGHT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
);

但这给了我一个错误Every derived table must have its own alias: SELECT SupplierPartNo, MajorEquipmentNo FROM( SELECT....

我怎样才能SELECT从以前的选择中做一个?

4

1 回答 1

1

如果您想要数据集中的这两个特定字段,您可以像这样直接查询:

SELECT SupplierInfo.SupplierPartNo, MajorEquipment.MajorEquipmentNo 
FROM SupplierInfo
LEFT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
UNION
SELECT SupplierInfo.SupplierPartNo, MajorEquipment.MajorEquipmentNo 
FROM SupplierInfo
RIGHT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId

如果您仍然想使用派生表方法(由于性能原因,我建议不要这样做),那么您可以像这样查询它:

SELECT a.SupplierPartNo, a.MajorEquipmentNo FROM (
SELECT * FROM SupplierInfo
LEFT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
UNION
SELECT * FROM SupplierInfo
RIGHT JOIN MajorEquipment ON SupplierInfo.SupplierId = MajorEquipment.MEId
) a
于 2014-03-21T05:33:08.573 回答