简体   繁体   English

在MySQL中用零替换空值

[英]Replacing null values with zeros in mysql

I know similar questions asked before, and I checked most of them. 我知道之前也曾问过类似的问题,并且我检查了大部分。 but I can't get this query to work. 但我无法使该查询正常工作。 I have several columns with null values, I want to display them as Zeros in the html table. 我有几列具有空值,我想在html表中将它们显示为零。 but I can't get the result i want. 但我无法获得想要的结果。

The query I make is this: 我的查询是这样的:

 $query = "SELECT Id, IFNULL(D0c,0), IFNULL(D1c,0), IFNULL(D2c,0), IFNULL(D3c,0) AS D0c, D1c, D2c, D3c FROM Findata Order by Id Desc Limit 100"; 

When I run the query I get these results: 当我运行查询时,我得到以下结果:

 ['889', , 1, 4, ],['888', , 1, 2, ],['887', 1, 1, 3, 1],['886', 2, 2, 1, 2],['885', , 1, 2, ], etc

Instead of Zeros, still null results are displayed. 代替零,仍然显示空结果。 I need a little help here please. 请给我一点帮助。 thanks. 谢谢。

-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 06, 2018 at 11:35 PM
-- Server version: 5.7.14
-- PHP Version: 5.6.25

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `Nexel`
--

-- --------------------------------------------------------

--
-- Table structure for table `temptable`
--

CREATE TABLE `temptable` (
  `D0c` varchar(5) DEFAULT NULL,
  `D1c` varchar(5) DEFAULT NULL,
  `D2c` varchar(5) DEFAULT NULL,
  `D3c` varchar(5) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `temptable`
--

INSERT INTO `temptable` (`D0c`, `D1c`, `D2c`, `D3c`) VALUES
('1', '2', '1', '1'),
('2', '1', '', '2'),
('2', '2', '1', ''),
('2', '2', '1', ''),
('1', '', '4', ''),
('', '2', '1', '2'),
('3', '', '1', '1'),
('', '2', '2', '1'),
('3', '1', '1', ''),
('2', '1', '', '2'),
('2', '2', '1', ''),
('1', '3', '1', ''),
('2', '', '2', '1'),
('', '2', '3', ''),
('1', '3', '1', ''),
('1', '2', '1', '1'),
('1', '3', '1', ''),
('2', '1', '2', ''),
('', '1', '2', '2'),
('1', '1', '2', '1'),
('1', '2', '1', '1'),
('3', '1', '', '1'),
('2', '2', '', '1'),
('2', '2', '1', ''),
('1', '', '4', ''),
('1', '4', '', ''),
('', '2', '', '3'),
('1', '', '3', '1'),
('2', '1', '2', ''),
('1', '2', '1', '1'),
('', '3', '1', '1'),
('2', '2', '1', ''),
('2', '1', '1', '1'),
('2', '1', '2', ''),
('2', '1', '2', ''),
('', '2', '1', '2'),
('', '1', '3', '1'),
('2', '1', '2', ''),
('', '1', '4', '');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

See update below 请参阅下面的更新

Instead of IFNULL, try COALESCE(): 尝试使用COALESCE()代替IFNULL:

SELECT 
    Id, 
    COALESCE(D0c,0) AS `FixedD0c`,
    COALESCE(D1c,0) AS `FixedD1c`,
    COALESCE(D2c,0) AS `FixedD2c`,
    COALESCE(D3c,0) AS `FixedD3c`,
    D0c,
    D1c, 
    D2c, 
    D3c 
FROM Findata 
Order by Id Desc 
Limit 100;

NOTE: BTW, in the query shown in your question, you seemed to be trying to assign column alias' all at once, but you merely renamed the results of the 4th column, and then included columns 2-4 again, without the IFNULLs. 注意:顺便说一句,在您的问题所示的查询中,您似乎试图一次全部分配列别名,但您只是重命名了第4列的结果,然后再次包含了2-4列,而没有IFNULL。 Your results from the query would not be as shown in your question, but would have had 7 columns in the output. 您从查询中获得的结果将不会与您的问题中所示的一样,但是在输出中将有7列。

EDIT: 编辑:

SQL Fiddle SQL小提琴

MySQL 5.6 Schema Setup : MySQL 5.6模式设置

CREATE TABLE `Findata` (
  `D0c` varchar(5) DEFAULT NULL,
  `D1c` varchar(5) DEFAULT NULL,
  `D2c` varchar(5) DEFAULT NULL,
  `D3c` varchar(5) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `temptable`
--

INSERT INTO `Findata` (`D0c`, `D1c`, `D2c`, `D3c`) VALUES
('1', '2', '1', '1'),
('2', '1', NULL, '2'),
('2', '2', '1', NULL),
('2', '2', '1', NULL),
('1', NULL, '4', NULL),
(NULL, '2', '1', '2'),
('3', NULL, '1', '1'),
(NULL, '2', '2', '1'),
('3', '1', '1', NULL),
('2', '1', NULL, '2'),
('2', '2', '1', NULL),
('1', '3', '1', NULL),
('2', NULL, '2', '1'),
(NULL, '2', '3', NULL),
('1', '3', '1', NULL),
('1', '2', '1', '1'),
('1', '3', '1', NULL),
('2', '1', '2', NULL),
(NULL, '1', '2', '2'),
('1', '1', '2', '1'),
('1', '2', '1', '1'),
('3', '1', NULL, '1'),
('2', '2', NULL, '1'),
('2', '2', '1', NULL),
('1', NULL, '4', NULL),
('1', '4', NULL, NULL),
(NULL, '2', NULL, '3'),
('1', NULL, '3', '1'),
('2', '1', '2', NULL),
('1', '2', '1', '1'),
(NULL, '3', '1', '1'),
('2', '2', '1', NULL),
('2', '1', '1', '1'),
('2', '1', '2', NULL),
('2', '1', '2', NULL),
(NULL, '2', '1', '2'),
(NULL, '1', '3', '1'),
('2', '1', '2', NULL),
(NULL, '1', '4', NULL);

Query 1 : 查询1

SELECT 
    COALESCE(D0c,0) AS `FixedD0c`,
    COALESCE(D1c,0) AS `FixedD1c`,
    COALESCE(D2c,0) AS `FixedD2c`,
    COALESCE(D3c,0) AS `FixedD3c`,
    D0c,
    D1c, 
    D2c, 
    D3c 
FROM Findata 
Limit 100

Results : 结果

| FixedD0c | FixedD1c | FixedD2c | FixedD3c |    D0c |    D1c |    D2c |    D3c |
|----------|----------|----------|----------|--------|--------|--------|--------|
|        1 |        2 |        1 |        1 |      1 |      2 |      1 |      1 |
|        2 |        1 |        0 |        2 |      2 |      1 | (null) |      2 |
|        2 |        2 |        1 |        0 |      2 |      2 |      1 | (null) |
|        2 |        2 |        1 |        0 |      2 |      2 |      1 | (null) |
|        1 |        0 |        4 |        0 |      1 | (null) |      4 | (null) |
|        0 |        2 |        1 |        2 | (null) |      2 |      1 |      2 |
|        3 |        0 |        1 |        1 |      3 | (null) |      1 |      1 |
|        0 |        2 |        2 |        1 | (null) |      2 |      2 |      1 |
|        3 |        1 |        1 |        0 |      3 |      1 |      1 | (null) |
|        2 |        1 |        0 |        2 |      2 |      1 | (null) |      2 |
|        2 |        2 |        1 |        0 |      2 |      2 |      1 | (null) |
|        1 |        3 |        1 |        0 |      1 |      3 |      1 | (null) |
|        2 |        0 |        2 |        1 |      2 | (null) |      2 |      1 |
|        0 |        2 |        3 |        0 | (null) |      2 |      3 | (null) |
|        1 |        3 |        1 |        0 |      1 |      3 |      1 | (null) |
|        1 |        2 |        1 |        1 |      1 |      2 |      1 |      1 |
|        1 |        3 |        1 |        0 |      1 |      3 |      1 | (null) |
|        2 |        1 |        2 |        0 |      2 |      1 |      2 | (null) |
|        0 |        1 |        2 |        2 | (null) |      1 |      2 |      2 |
|        1 |        1 |        2 |        1 |      1 |      1 |      2 |      1 |
|        1 |        2 |        1 |        1 |      1 |      2 |      1 |      1 |
|        3 |        1 |        0 |        1 |      3 |      1 | (null) |      1 |
|        2 |        2 |        0 |        1 |      2 |      2 | (null) |      1 |
|        2 |        2 |        1 |        0 |      2 |      2 |      1 | (null) |
|        1 |        0 |        4 |        0 |      1 | (null) |      4 | (null) |
|        1 |        4 |        0 |        0 |      1 |      4 | (null) | (null) |
|        0 |        2 |        0 |        3 | (null) |      2 | (null) |      3 |
|        1 |        0 |        3 |        1 |      1 | (null) |      3 |      1 |
|        2 |        1 |        2 |        0 |      2 |      1 |      2 | (null) |
|        1 |        2 |        1 |        1 |      1 |      2 |      1 |      1 |
|        0 |        3 |        1 |        1 | (null) |      3 |      1 |      1 |
|        2 |        2 |        1 |        0 |      2 |      2 |      1 | (null) |
|        2 |        1 |        1 |        1 |      2 |      1 |      1 |      1 |
|        2 |        1 |        2 |        0 |      2 |      1 |      2 | (null) |
|        2 |        1 |        2 |        0 |      2 |      1 |      2 | (null) |
|        0 |        2 |        1 |        2 | (null) |      2 |      1 |      2 |
|        0 |        1 |        3 |        1 | (null) |      1 |      3 |      1 |
|        2 |        1 |        2 |        0 |      2 |      1 |      2 | (null) |
|        0 |        1 |        4 |        0 | (null) |      1 |      4 | (null) |

Instead of 代替

IFNULL(D0c,0)

Try 尝试

REPLACE(Doc,NULL,0)

如果为空字符串:

$query = "SELECT  Id, CASE when D0c like '' then '0' else D0c end as D0c, CASE when D1c like '' then '0' else D1c end as D1c, CASE when D2c like '' then '0' else D2c end as D2c, case when D3c like '' then '0' else D3c end as D3c FROM Mytable Order by Id Desc Limit 100" ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM