【问题标题】:How to set column value as zero if value is empty using php/mysqli?如果使用 php/mysqli 值为空,如何将列值设置为零?
【发布时间】:2018-10-25 16:43:04
【问题描述】:

连接两列时(3,4)。如果第一个值为空或 null,则为零(0,4),如果第二个值为空或 null,则为零,如 3,0。如何在 sql 查询中使用php.

CONCAT_WS(',',IFNULL(a.PD_right, ''),IFNULL(a.PD_left, '')) as PD_RL

在上面的代码中,连接PD_right和PD_left。

现在越来越喜欢了

  1. 如果两个值为空,则显示为 (,) 仅逗号。

  2. 如果有两个值,它将显示为 3,4

  3. 如果第一个值为空,它将显示为 ,4

  4. 如果第二个值为空,它将显示为 3,

如何解决这些问题?我的数据库列是空白的。它不是 NULL 或 null

【问题讨论】:

  • PD_right、PD_left是什么类型?
  • 两列的数据类型都是 varchar(20)
  • 如果我理解正确,只需将空字符串替换为 '0' 即可,CONCAT_WS(',',IFNULL(a.PD_right, '0'),IFNULL(a.PD_left, '0')) as PD_RL
  • 我试过了,但不知道为什么

标签: php mysql sql


【解决方案1】:

如果我理解正确,只需像 CONCAT_WS(',',IFNULL(a.PD_right, '0'),IFNULL(a.PD_left, '0')) as PD_RL 一样将空字符串替换为 '0' 即可。

如果您想检测 NULL 或空字符串 '',请尝试使用 IF 函数。

我在我的机器上做了简单的测试,输出如下:

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12 Homebrew

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost) [(none)]> SET @left = 3, @right = 4;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> SELECT CONCAT_WS(',', IF(@left IS NULL OR @left = '', '0', @left), IF(@right IS NULL OR @right = '', '0', @right)) as PD_RL;
+-------+
| PD_RL |
+-------+
| 3,4   |
+-------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> SET @left = 3, @right = NULL;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> SELECT CONCAT_WS(',', IF(@left IS NULL OR @left = '', '0', @left), IF(@right IS NULL OR @right = '', '0', @right)) as PD_RL;
+-------+
| PD_RL |
+-------+
| 3,0   |
+-------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> SET @left = NULL, @right = 4;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> SELECT CONCAT_WS(',', IF(@left IS NULL OR @left = '', '0', @left), IF(@right IS NULL OR @right = '', '0', @right)) as PD_RL;
+-------+
| PD_RL |
+-------+
| 0,4   |
+-------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> SET @left = NULL, @right = NULL;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> SELECT CONCAT_WS(',', IF(@left IS NULL OR @left = '', '0', @left), IF(@right IS NULL OR @right = '', '0', @right)) as PD_RL;
+-------+
| PD_RL |
+-------+
| 0,0   |
+-------+
1 row in set (0.00 sec)

(root@localhost) [(none)]> SET @left = '', @right = '';
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]> SELECT CONCAT_WS(',', IF(@left IS NULL OR @left = '', '0', @left), IF(@right IS NULL OR @right = '', '0', @right)) as PD_RL;
+-------+
| PD_RL |
+-------+
| 0,0   |
+-------+
1 row in set (0.00 sec)

【讨论】:

    【解决方案2】:

    更改这行查询:

      CONCAT_WS(',',IFNULL(a.PD_right, ''),IFNULL(a.PD_left, '')) as PD_RL
    

      CONCAT_WS(',',IFNULL(a.PD_right, 0),IFNULL(a.PD_left, 0)) as PD_RL
    

    或者,您可以使用CASE ...WHEN...THEN...END 语法在连接之前测试这些值。

    查看更多here

    【讨论】:

    • 在我的数据库中的列是空白的。如果这个代码不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2014-10-03
    相关资源
    最近更新 更多