SQL SELECT和UPDATE返回不同的结果

时间:2013-03-04 10:04:08

标签: sql

我可能需要你的帮助...... 我有以下MySQL查询:

SELECT * FROM dbName1.Prospects A
LEFT JOIN dbName2.inbox B ON 
          substring(A.ProspectCellPhone, 2) = substring(B.SenderNumber, 4) OR 
          substring(A.ProspectCellPhone, 3) = substring(B.SenderNumber, 4) OR 
          substring(A.ProspectCellPhone, 4) = substring(B.SenderNumber, 4) 
LEFT JOIN dbName1.User C ON A.Username = C.Username
WHERE B.TextDecoded IS NOT NULL
AND substring(B.SenderNumber,1,1) = '+'
AND B.NotificationFlag = '0'

返回125结果... 但是当我尝试使用相同的查询进行更新时,它只返回45个更新结果... 更新查询是:

UPDATE dbName1.Prospects A
LEFT JOIN dbName2 B ON 
            substring(A.ProspectCellPhone, 2) = substring(B.SenderNumber, 4) OR 
            substring(A.ProspectCellPhone, 3) = substring(B.SenderNumber, 4) OR 
            substring(A.ProspectCellPhone, 4) = substring(B.SenderNumber, 4) 
LEFT JOIN dbName1.User C ON A.Username = C.Username
SET B.NotificationFlag = '1'
WHERE B.TextDecoded IS NOT NULL
AND substring(B.SenderNumber,1,1) = '+'
AND B.NotificationFlag = '0'

我在UPDATE查询上做错了吗?

非常感谢你的帮助......

3 个答案:

答案 0 :(得分:0)

尝试使用相同的语句,在您的情况下,第二行在两个语句中都不同:

在Select语句中,它是:

LEFT JOIN dbName2.inbox B ON 

在Update语句中,它是:

LEFT JOIN dbName2 B ON 

答案 1 :(得分:0)

运行此查询以检查表B中已加入的实际记录数。

Select distinct B.some_unique_field from dbName2.inbox B
Left Join dbName1.Prospects A ON 
          substring(A.ProspectCellPhone, 2) = substring(B.SenderNumber, 4) OR 
          substring(A.ProspectCellPhone, 3) = substring(B.SenderNumber, 4) OR 
          substring(A.ProspectCellPhone, 4) = substring(B.SenderNumber, 4) 
WHERE B.TextDecoded IS NOT NULL
AND substring(B.SenderNumber,1,1) = '+'
AND B.NotificationFlag = '0';

希望我的查询是正确的:)

答案 2 :(得分:0)

您的选择与udpate不匹配,因为您正在对b进行更新,因此它基本上会缩减为a JOIN b,而选择为a LEFT JOIN b LEFT JOIN C。 A中的多行也可以匹配B中的一行,因此您可以获得比B中不同行更多的结果。试试这个:

SELECT DISTINCT b.ID FROM dbName1.Prospects A
JOIN dbName2.inbox B ON 
substring(A.ProspectCellPhone, 2) = substring(B.SenderNumber, 4) OR 
substring(A.ProspectCellPhone, 3) = substring(B.SenderNumber, 4) OR 
substring(A.ProspectCellPhone, 4) = substring(B.SenderNumber, 4) 
WHERE B.TextDecoded IS NOT NULL
AND substring(B.SenderNumber,1,1) = '+'
AND B.NotificationFlag = '0'