我正在尝试使用PHP中的LIKE
和NOT LIKE
来查询结果。的MySQL。
我有一个包含2列的表BeaconEvent
:BeaconID
和EventID
;
表 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%"
是否有任何建议来比较不等于特定值的值?提前致谢
答案 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 ║
╚═══════════╝