我需要比較從目的地A到目的地B的貨運數量。有兩個表,1和2。表1有目的地A和目的地B列。表2有一個主鍵代碼,與表1的Destination A和Destination B兩列相聯系。我的問題是,我不確定如何將表2的主鍵中的一個特定值分配給 "Destination A",以及將表2的主鍵中的另一個特定值分配給 "DestinationB".
。例如:
SELECT *
FROM table1
LEFT JOIN table 2
ON table1.destinationA = table2_primarykey
WHERE table2.destination = "芝加哥"
這將顯示表1中所有目標鍵等同于芝加哥市的記錄,這是我想做的事情的一半。另一半是將表1中的特定主鍵值設定為 "目的地B"。
SELECT *
FROM table1
LEFT JOIN table 2
ON table1.destinationA = table2_primarykey and table1.destinationB = table2_primarykey
WHERE table2.destination in ('Chicago','Seattle')
在上面的查詢中,我試圖找出有多少貨物從出發地芝加哥到目的地西雅圖。上面的連接條件對我不起作用,因為它只顯示芝加哥到芝加哥和西雅圖到西雅圖。
有什么方法可以解決這個問題嗎?
uj5u.com熱心網友回復:
嘗試使用TABLE2兩次,每次都與TABLE1中相應的目的地列連接,并在where部分選擇所需的目的地。
SELECT * FROM TABLE1 A
JOIN TABLE2 B ON A.DestinationA = B.PrimaryKey
JOIN TABLE2 C ON A.DestinationB = C.PrimaryKey
WHERE B.Destination = 'Chicago' and C. 目的地= 'Seattle' 'Seattle'
這個查詢將映射與DestinationA和DestinationB相關的Destination(來自TABLE2),就好像這些是來自不同的表,在WHERE部分選擇相應的destination應該會得到期望的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/314799.html
標籤:
