PhP MySQL NOT LIKE不会返回正确的值

时间:2016-01-02 05:54:14

标签: php mysql sql compare sql-like

我正在尝试使用PHP中的LIKENOT LIKE来查询结果。的MySQL。

我有一个包含2列的表BeaconEventBeaconIDEventID;

BeaconEvent

BeaconID        EventID
-------------------------
5DBA-BC5B       Time0003
5DBA-BC5B       NLB0001
5264-9028       NLB0004
1F8B-35E6       NLB0005

当我运行MySQL命令选择所有BeaconID EventID是一个特定值时,查询工作正常,但当我运行查询以选择BeaconID EventID时}不等于指定值,它返回所有BeaconID(不正确,应该排除BeaconID,其中EventID等于特定值)

有效的示例查询:

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID = "Time0003"

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID LIKE "Time0003"

这些查询不起作用:

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID != "Time0003"

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID <> "Time0003"

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID NOT LIKE "Time0003"

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID NOT LIKE "%Time0003%"

是否有任何建议来比较不等于特定值的值?提前致谢

1 个答案:

答案 0 :(得分:3)

您的数据对于两个值具有相同的BeaconID

BeaconID        EventID
5DBA-BC5B       Time0003       
5DBA-BC5B       NLB0001   

您的查询完美应用:

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID <> "Time0003";

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.EventID = "Time0003";

SqlFiddleDemo

输出:

╔═══════════╗
║ BeaconID  ║
╠═══════════╣
║ 5DBA-BC5B ║
║ 5264-9028 ║
║ 1F8B-35E6 ║
╚═══════════╝

╔═══════════╗
║ BeaconID  ║
╠═══════════╣
║ 5DBA-BC5B ║
╚═══════════╝

如果您想要排除它(并且您确定数据是正确的),您可以使用:

SELECT BeaconEvent.BeaconID 
FROM BeaconEvent 
WHERE BeaconEvent.BeaconID <> (SELECT BeaconID 
                               FROM BeaconEvent
                               WHERE EventID = 'Time0003')

SqlFiddleDemo2

输出:

╔═══════════╗
║ BeaconID  ║
╠═══════════╣
║ 5264-9028 ║
║ 1F8B-35E6 ║
╚═══════════╝