Empty IN clause parameter list in MySQL(MySQL 中的空 IN 子句参数列表)
问题描述
当您执行 IN 子句为空的 SQL 查询时会发生什么?
What happens when you do a SQL query where the IN clause is empty?
例如:
SELECT user WHERE id IN ();
MySQL 是否会按预期处理此问题(即始终为 false),如果没有,我的应用程序在动态构建 IN 子句时如何处理这种情况?
Will MySQL handle this as expected (that is, always false), and if not, how can my application handle this case when building the IN clause dynamically?
推荐答案
如果我有一个应用程序,我正在动态构建 IN 列表,但它可能最终为空,我有时会这样做使用不可能的值初始化列表并添加到该列表中.例如.如果它是用户名列表,我将从一个空字符串开始,因为这不是一个可能的用户名.如果是 auto_increment ID,我会使用 -1 因为实际值总是正的.
If I have an application where I'm building the IN list dynamically, and it might end up empty, what I sometimes do is initialize the list with an impossible value and add to that. E.g. if it's a list of usernames, I'll start with an empty string, since that's not a possible username. If it's an auto_increment ID, I'll use -1 because the actual values are always positive.
如果这不可行,因为没有不可能的值,您必须使用条件来决定是否在 WHERE中包含 AND column IN ($values) 表达式代码>子句.
If this isn't feasible because there are no impossible values, you have to use a conditional to decide whether to include AND column IN ($values) expression in the WHERE clause.
这篇关于MySQL 中的空 IN 子句参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 中的空 IN 子句参数列表
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
