我试图在PHP和MySQL中使用LIKENOT LIKE来查询结果。
我有一张表,有两列:BeaconEventBeaconID
表BeaconEvent:

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

当我运行MySQL命令select allEventIDwithBeaconID是一个特定值时,查询工作正常,但当我运行查询selectEventIDwithBeaconID不等于指定值时,它将返回所有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%"

有什么建议可以比较不等于某一特定值的值吗?提前谢谢

最佳答案

对于两个值,您的数据具有相同的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 ║
╚═══════════╝

07-26 00:00