0

表 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;
4

2 回答 2

2

两者之间的共同字段是userIdentity应该在ON子句之后JOIN而不是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

检查我的小提琴演示

于 2013-11-08T05:09:53.320 回答
1

我很确定你的“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;
于 2013-11-08T05:03:53.290 回答