Mysql中if函数的使用

IF function is used for conditional judgment in MySQL. 语法为 IF(expr1 ,expr2 ,expr3 )。
Returns expr2 if expr1 is true, otherwise returns expr3 . 例如统计子表t的用户数据。
Use the IF function to handle the situation where user_id is empty. When user_id is empty, first_time, total_amount and new_amount are all 0. The counting function counts the number of records. count() counts all columns and returns the number of records. For example, table t has 1 00 records and count() returns 1 00. count(1 ) or count(2 ), etc., uses 1 instead of the column value statistics. count(column name) counts non-NULL values ​​in the specified column. count(distinguished column name) counts different values. Execution efficiency: Using count (column name) for the primary key column is highly efficient. It is efficient to use count(1 ) for non-primary key columns. For tables with primary keys, it is best to use primary key columns for statistics. It is more efficient to use count() for a single column. For multi-column tables without primary keys, count(1 ) is better than count(). To count the number of records with a NULL value, use the count()+where clause. For example, select count() from t where user_id is null. count('') 返回记录数。
count(0) 还返回记录数。
count(null) 返回 0。
你自己掂量一下吧。

MySQL ISNULL 函数详解及用法介绍

MySQL中的find_in_set()函数使用技巧心得与应用场景总结

MySQL查询字段为空时替换(补充)为0

如果有的话啊,这个IFNULL,我过去了。

我记得2 02 2 年他们在北京做一个东西。
里面有一个叫order_detail的表和一个叫quantity的字段。
这个数量有时是空的,不方便检查。
我想使用 IFNULL。

所以,我写了一条SQL,就是:SELECT IFNULL(quantity, 0) AS amount FROM order_detail;面子才是最重​​要的,数量才是世界上最重要的部分。
这是唯一的办法。

IFNULL 非常简单。
请使用它。
如果是NULL要给的值)。

当时很困惑。
起初我写了 IFNULL(quantity, '0'),但后来我意识到我错了。
后来我明白了,数量是一个数字,你想要给予的也是一个数字。
由于不能是字符串‘0’,所以必须直接写0。










































< br>



























<<<没有。
因此,您应该使用 CASE。

例如,我又写了一条SQL: SELECT CASE WHEN note = '' OR note IS NULL THEN 0 ELSE amount END AS amount FROM order_detail;这样,如果 note 是空字符串或 NULL,则提供 0,否则提供数量的原始值。

From CAST to CONVERT Convert 换关系不大。
Character 符串或者整数,Confucius 符串或者特算算“Gong”,Thoughts 处理这个,CASE 或者IF 更合适。

In short, IFNULL is very good and simple.它很容易使用。
这取决于具体情况。
选择正确的方法取决于具体情况。
当我输入数量时,IFNFULL 显示为 NULL。