SQL Join - 同一个表中的两个外键引用主键

时间:2016-09-08 18:24:16

标签: sql-server tsql join

我有两个表,一个是产品转换历史表,另一个是产品表:

dbo.Product_Change_History

Date  FK_ProductID FK_FinalProductID ChangedBy
20160709    1               3           John

dbo.Product_Detail

PK_ProductID ProductName 
     1        Red Bike
     3        Green Bike

我想要一张像这样的表:

 Date      ProductName FinalProductName
20160709   Red Bike     Green Bike

如何加入这两个表来实现这一目标? 编辑:这是在TSQL中

4 个答案:

答案 0 :(得分:2)

您必须加入两次,这意味着您至少要为其中一个连接添加别名:

SELECT Date, foo.ProductName, bar.FinalProductName
FROM Product_Change_history
LEFT JOIN Product_Detail AS foo ON fk_productid = foo.pk_productid
LEFT JOIN Product_Detail AS bar ON fk_finalproductid = bar.pk_productid

答案 1 :(得分:0)

我不确定LEFT加入VS INNER。我认为这样会很好。

SELECT
 link.Date,
 old.ProductName,
 new.ProductName as FinalProductName
FROM
 dbo.Product_Detail as old
 INNER JOIN dbo.Product_Change_History as link
  ON old.PK_ProductID = link.FK_ProductID
 INNER JOIN dbo.Product_Detail as new
  ON new.PK_ProductID = link.FK_FinalProductID

答案 2 :(得分:0)

private var PlayerObserverContext = 0
private var PlayerItemObserverContext = 0
private var PlayerLayerObserverContext = 0

    override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    switch (keyPath, context) {
    case (.some(PlayerRateKey), PlayerObserverContext):
        true
    case (.some(PlayerStatusKey), PlayerItemObserverContext):
        true
    case (.some(PlayerKeepUp), PlayerItemObserverContext):
        if let item = self.playerItem {
            self.bufferingState = .ready
            self.delegate?.playerBufferingStateDidChange(self)

            if item.isPlaybackLikelyToKeepUp && self.playbackState == .playing {
                self.playFromCurrentTime()
            }
        }

        let status = (change?[NSKeyValueChangeKey.newKey] as! NSNumber).intValue as AVPlayerStatus.RawValue

        switch (status) {
        case AVPlayerStatus.readyToPlay.rawValue:
            self.playerView.playerLayer.player = self.player
            self.playerView.playerLayer.isHidden = false
        case AVPlayerStatus.failed.rawValue:
            self.playbackState = PlaybackState.failed
            self.delegate?.playerPlaybackStateDidChange(self)
        default:
            true
        }
    case (.some(PlayerEmptyBufferKey), PlayerItemObserverContext):
        if let item = self.playerItem {
            if item.isPlaybackBufferEmpty {
                self.bufferingState = .delayed
                self.delegate?.playerBufferingStateDidChange(self)
            }
        }

        let status = (change?[NSKeyValueChangeKey.newKey] as! NSNumber).intValue as AVPlayerStatus.RawValue

        switch (status) {
        case AVPlayerStatus.readyToPlay.rawValue:
            self.playerView.playerLayer.player = self.player
            self.playerView.playerLayer.isHidden = false
        case AVPlayerStatus.failed.rawValue:
            self.playbackState = PlaybackState.failed
            self.delegate?.playerPlaybackStateDidChange(self)
        default:
            true
        }
    case (.some(PlayerReadyForDisplay), PlayerLayerObserverContext):
        if self.playerView.playerLayer.isReadyForDisplay {
            self.delegate?.playerReady(self)
        }
    default:
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)

    }

}

答案 3 :(得分:0)

感谢您的回复,在Chad Zinks的帮助下,我找到了正确的解决方案:

select
PRDHIST.Date,
OLDPROD.ProductName,
NEWPROD.ProductName
from dbo.Product_ChangeHistory PRDHIST
inner join dbo.Product_Detail OLDPROD
on OLDPROD.PK_ProductID = PRDHIST.FK_ProductID
inner join dbo.Product_Detail NEWPROD
on NEWPROD.PK_ProductID = PRDHIST.FK_FinalProductID;