我已经到了需要在 MySQL 中执行一个看起来非常复杂的查询的地步。基本上,我有一个表是事件列表:
eventid | businessid | category | type | subtype | start | end
获取指定时间内的事件很容易。但是,用户也可以使用 businessid、category、type 或 subtype 字段阻止事件显示。这些列在“阻止”表中...
userid | businessid | category | type | subtype
此表将只列出他们的用户 ID 以及被阻止的项目中的信息
00001 | b00001 | null | null | null
00001 | null | nightlife | null | null
等等……
本来我以为我可以使用连接来获取信息,但是由于有多个类别,所以这行不通。我想我可以为每种类型的 block 创建一个数组,并在每次迭代时针对它测试查询,但似乎应该有一种方法可以将其用于查询本身。有任何想法吗?感谢您的帮助。
** 注意:如果需要,我可以更改表格的布局。
最佳答案
我只是创建一个 NOT EXISTS
子句,使用 any 在 blocked
中匹配行的谓词。
SELECT e.* FROM `events` e
WHERE NOT EXISTS (
SELECT 1 FROM `blocked` b
WHERE b.userid = :user_id -- this is a parameter for the user ID
AND (
e.businessid = b.businessid
OR e.category = b.category
OR e.type = b.type
OR e.subtype = b.subtype
)
)
关于php - 从 MySQL 中获取未在另一个表中被阻止的事件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331882/