mysql - SQL查询 "like"运算符错误,当字段为空时

标签 mysql sql string sql-like sql-null

此查询返回 421 条记录:

this query returns 421 records

现在,当我实现“like”运算符时,它返回 349 条记录,它没有考虑字段“invoice_number”为 null 的记录:

Now when I implement the "like" operator, it returns 349 records, it is not taking into account the records with the field "invoice_number" in null

最佳答案

类似于 null like '%%' 的条件是假的。 null不是like任何事物。所以如果你想允许 null值(value)观,你需要明确它:

and (ds.numero_factura like '%%' or ds.numero_factura is null)

或者您可以使用coalesce() :

 and coalesce(ds.numero_factura, '') like '%%'

这个条件实际上没有意义(基本上它会允许所有记录),因此您需要根据您的具体用例进行调整。

另一句话是ds.numero_factura看起来像一个数字;在这种情况下,您不想对其使用字符串函数:请改用数字函数。

关于mysql - SQL查询 "like"运算符错误,当字段为空时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59648613/

相关文章:

mysql - 限制mysql获取数组?

sql - SQL Server 上的预留空间过多

php - 按下按钮时尝试发送主键值以编辑数据

MySQL 字段值,可以定义超出数字计数的 float 吗?

php - 在 PHP 文档中使用 SQL 查询数据库中的 2 个不同表时遇到困难

python - 如何使用 pyodbc 和 MS-Access 在 Python cursor.execute 中查看真正的 SQL 查询

python - 匹配除模式以外的所有内容并替换为字符串

python - 将一列中的值替换为另一列的特定实例

c++ - 如何比较两个字符串?

mysql - MySql 查询浏览器的替代品?