[英]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.