English 中文(简体)
我sql - 功能,如零值检查
原标题:mysql - function like isnull() to check for zero-value

I m looking for a function to check if a column has the value 0 that behaves like the isnull() function.

This is what I ve got so far:

CONCAT(
    SUBSTRING_INDEX(
        SUBSTRING(textCol, 1, 
            LOCATE(DATE_FORMAT(dateCol, %d.%m.%Y  %H:%i ), textCol)-1),
             | ,
             -1),       
    SUBSTRING(
        textCol,
        LOCATE(DATE_FORMAT(dateCol, %d.%m.%Y  %H:%i ), textCol),
        IFNULL(
            LOCATE( /* this will return 0 if it s the last entry */
                 | , 
                textCol, 
                LOCATE(DATE_FORMAT(dateCol, %d.%m.%Y  %H:%i ), textCol)
            ) - LOCATE(DATE_FORMAT(dateCol, %d.%m.%Y  %H:%i ), textCol),
            LENGTH(textCol))))

The data in textCol is sort of like a CSV-file with another CSV-file inside each column. Each dataset is delimited by | while each dataset inside that is delimited by &&. The inner fields are name, date like %d.%m.%Y %H:%i and a comment.

i 想要提取与<代码> 日期Col中某一数字表示相对应的数据集。 问题是,如果一个Im 查询是Coltext/code>的最后条目,则LOCATE(见评论)回报。 0. 但是,这打破了我的<代码>SUBSTRING,因此只有上半个条目被退回。

I added a ISNULL(), which of course doesn t do anything here. What I would like to do is have something similar to the ISNULL() that sort of does a ISZERO(), much like the || operator in Perl behaves.

There might be an alltogether better way to achieve what I want. Please don t hesitate to tell me.

最佳回答
SELECT  CASE expression WHEN 0 THEN  a substitute for zero  ELSE expression END
FROM    mytable
问题回答

这里可以有所助益:如果表达是全国人民联盟的或<>>,就会有很强的检查方式。 0:

IFNULL(NULLIF(expr, 0), alt_expr)

alt_expr,expr为NUL或0,否则返回expr

缩短时间,避免重复表达:

SELECT COALESCE( NULLIF( expression, 0 ),  a substitute for zero  ) FROM mytable




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签