簡體   English   中英

我的mysql JOIN怎么了,我沒有得到正確的結果

[英]Whats wrong with my mysql JOIN, I am not getting the right results

表A具有userIdentity(primaryKey)userName userPhoneNumber

表B具有someOtherKey(primaryKey)userIdentity userLocation

運行時,我得到22行具有唯一的userIdentity

SELECT *
FROM tableB 
WHERE tableB.timeStamp > '2013-11-1' 
GROUP BY tableB.userIdentity ;

我運行時僅得到2行(我打算在上述查詢中提取22行的名稱和電話號碼)

SELECT tableA.userName, tableA.phonePhoneNumber 
FROM tableB
JOIN tableA 
WHERE tableB.timeStamp > '2013-11-1'
GROUP BY tableB.userIdentity
    AND tableA.identityHash = tableB.identityHash;

兩者之間的公共字段是userIdentity ,該字段應位於JOIN之后的ON子句中,而不是identityHash ,例如:

 SELECT tableA.userName, tableA.phonePhoneNumber 
 FROM tableB
 JOIN tableA
 ON tableB.userIdentity = tableA.userIdentity 
 WHERE tableB.timeStamp > '2013-11-1'
 GROUP BY tableB.userIdentity

檢查我的小提琴演示

我很確定您的“ AND”子句在錯誤的位置。 您可能打算做的是:

SELECT tableA.userName, tableA.phonePhoneNumber 
FROM tableB JOIN tableA 
WHERE tableB.timeStamp > '2013-11-1'
    AND tableA.identityHash = tableB.identityHash
GROUP BY tableB.userIdentity;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM