MySQL中的空IN子句参数列表 [英] Empty IN clause parameter list in MySQL

查看:578
本文介绍了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子句参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆