sql - 如何从有条件的数据库中获取行 - 另一个表中存在另一个?
问题描述
我有两张桌子notifications
和archived_notifications
.
通知
id | title | caption | user_id
存档通知
notification_id | user_id
如果我想从notifications
表中获取表中没有行的行,archived_notifications
如notification_id = notification.id | user_id = notification.user_id
.
现在我有一些这样的
cursor.execute("SELECT * FROM notifications AS n WHERE "
"({}=%s AND (SELECT COUNT(*) FROM archvies WHERE {}=n.id AND {}=%s) = 0) "
"OR "
"({}=%s AND (SELECT COUNT(*) FROM archvies WHERE {}=n.id AND {}=%s) = 0) "
"ORDER by id DESC LIMIT %s OFFSET %s"
.format(Keys.USER_ID, Keys.NOTIFICATION_ID, Keys.USER_ID, Keys.DIRECTION, Keys.NOTIFICATION_ID,
Keys.USER_ID),
[str(user_id), str(user_id), NotificationsClasses.GLOBAL, str(user_id), int(limit), int(offset)])
解决方案
通常,您可以使用NOT IN、NOT EXISTS 和 LEFT JOIN子句中的任何一个。具体来说,您似乎还有许多其他不清楚的条件,例如来自未知Keys变量的列名的方向处理和字符串格式。
下面显示了NOT EXISTS
尝试翻译当前代码的选项。根据实际需要调整:
sql = '''SELECT * FROM notifications AS n
WHERE n.user_id = %s
AND NOT EXISTS
(SELECT 1 FROM archives a
WHERE a.user_id = %s
AND n.id = a.notification_id)
ORDER by n.id DESC
LIMIT %s
OFFSET %s
'''
cursor.execute(sql, [str(user_id), NotificationsClasses.GLOBAL, int(limit), int(offset)])
推荐阅读
- c# - MongoDB C# Methode 搜索文档(T 型文档)
- python - 在循环中增加索引的开始和结束边界
- amazon-web-services - 我希望能够将 S3 上的目录下载为压缩文件
- python - Python Caesar 密码问题用高密钥解密
- javascript - 如何在 React JS 中组合具有相同日期的数组
- parsing - LR(1) - 当有 epsilon 产生时,我如何知道要从节点堆栈中弹出多少项?
- r - 如何编写一个创建模型并具有引用同一模型的函数的 for 循环
- database - mongodb 使用 $nin 分片键进行分片
- reactjs - ESLint: 'React' 已定义但从未使用过。(no-unused-vars) 使用 JSX pragma 时
- androidx - 导航组件 - 使用多个堆栈处理返回行为