我的MySQL查询(UNION)有什么问题?

时间:2016-01-21 08:03:45

标签: mysql join union

我希望你能把我送到这里。我正在研究PMS系统,我想运行一个简单的SELECT语句,根据客户的哪些包裹到达以及交易类型,获取客户的密码列表。 'transferConfirmation'表已经有客户ID和密码,并为每个实例显示一行但由于某种原因,当我使用以下查询时:

SELECT `tc`.`transferID` AS `transferID`,
       `c`.`firstName` AS `firstName`,
       `c`.`lastName` AS `lastName`,
       `tc`.`secretCode` AS `secretCode`,
       'Purchase Order' AS `transactionType`
FROM (((((`db`.`transferconfirmation` `tc`
          JOIN `db`.`customer` `c` on((`tc`.`customerID` = `c`.`customerID`)))
         JOIN `db`.`transfer` `t` on((`t`.`transferID` = `tc`.`transferID`)))
        JOIN `db`.`transferentry` `te` on((`t`.`transferID` = `te`.`transferID`)))
       JOIN `db`.`purchaseorder` `po` on((`te`.`referenceID` = `po`.`purchaseOrderID`)))
      JOIN `db`.`purchaseorderentry` `poe` on(((`po`.`purchaseOrderID` = `poe`.`purchaseOrderID`)
                                               AND (`te`.`referenceEntryID` = `poe`.`purchaseOrderEntryID`))))
WHERE (`poe`.`collectionStatus` = 0)
UNION ALL
SELECT `tc`.`transferID` AS `transferID`,
       `c`.`firstName` AS `firstName`,
       `c`.`lastName` AS `lastName`,
       `tc`.`secretCode` AS `secretCode`,
       'Shipping Order' AS `transactionType`
FROM (((((`db`.`transferconfirmation` `tc`
          JOIN `db`.`customer` `c` on((`tc`.`customerID` = `c`.`customerID`)))
         JOIN `db`.`transfer` `t` on((`t`.`transferID` = `tc`.`transferID`)))
        JOIN `db`.`transferentry` `te` on((`t`.`transferID` = `te`.`transferID`)))
       JOIN `db`.`shippingorder` `so` on((`te`.`referenceID` = `so`.`shippingOrderID`)))
      JOIN `db`.`shippingorderentry` `soe` on(((`so`.`shippingOrderID` = `soe`.`shippingOrderID`)
                                               AND (`te`.`referenceEntryID` = `soe`.`shippingOrderEntryID`))))
WHERE (`soe`.`collectionstatus` = 0)

我不断获得重复数据的结果只与交易有区别。我想我的问题是,如果密码被选中一次,如何阻止任何其他行显示相同的代码。

1 个答案:

答案 0 :(得分:0)

使用public class ProductMap : EntityTypeConfiguration<Product> { public ProductMap() { this.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("Id"); // Complex type mapping here? } } 运算符。 UNION运算符将从每个查询中删除重复记录(UNION将保留它们)。

UNION ALL