简体   繁体   中英

mysql concat_ws with the result of a custom function prevents like '%str%' from working

So I have the following sql that works:

SELECT CONCAT_WS('',`brand`,`pattern`,`product_code`,`size`,`normal_price`,`sale_price`,`text_special`,`load_index`,`speed_index`,`id`) `all_columns` FROM baz_tyres
    HAVING (`all_columns`  LIKE '%con%')
    ORDER BY brand asc
    LIMIT 0, 10

This works fine returning any record that contains the search string in any column such as "ContinentalContiCrossContact® LX21549340225/65R17."

But then I also wanted to be able to match just the numerical value from size so I added the custom function I wound here: How to get only Digits from String in mysql?

DELIMITER $$

CREATE FUNCTION `ExtractNumber`(in_string VARCHAR(50)) 
RETURNS INT
NO SQL
BEGIN
    DECLARE ctrNumber VARCHAR(50);
    DECLARE finNumber VARCHAR(50) DEFAULT '';
    DECLARE sChar VARCHAR(1);
    DECLARE inti INTEGER DEFAULT 1;

    IF LENGTH(in_string) > 0 THEN
        WHILE(inti <= LENGTH(in_string)) DO
            SET sChar = SUBSTRING(in_string, inti, 1);
            SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9'); 
            IF ctrNumber > 0 THEN
                SET finNumber = CONCAT(finNumber, sChar);
            END IF;
            SET inti = inti + 1;
        END WHILE;
        RETURN CAST(finNumber AS UNSIGNED);
    ELSE
        RETURN 0;
    END IF;    
END$$

DELIMITER ;

Now that I have this function I also want to concat the resulting number and use it for the search.

So I added ExtractNumber( size ) into the congat

SELECT CONCAT_WS('',ExtractNumber(`size`),`brand`,`pattern`,`product_code`,`size`,`normal_price`,`sale_price`,`text_special`,`load_index`,`speed_index`,`id`) `all_columns` FROM baz_tyres
HAVING (`all_columns`  LIKE '%con%')
ORDER BY brand asc
LIMIT 0, 10

When the function is involved the like search fails to find any matches. However if I change the havving to a where condition checking for a specific brand name...

SELECT CONCAT_WS('',ExtractNumber(`size`),`brand`,`pattern`,`product_code`,`size`,`normal_price`,`sale_price`,`text_special`,`load_index`,`speed_index`,`id`) `all_columns` FROM baz_tyres
WHERE (`brand`  LIKE '%con%')
ORDER BY brand asc
LIMIT 0, 10

Then I can see that indeed the concat with the function inside does actually work returning "2147483647ContinentalVancoFourSeason 20473361205/7...." but performing a like on this resulting string doesn't match when it should.

Some columns have special chars and I have tried casting the function result to utf8 and it had no effect.

Any ideas why I cant do a like on this concat string?

UPDATE: It's working now.. I had to put the conversion inside the iterator function itself.

RETURN CAST(finNumber AS CHAR);

For some reason convert or cast with the function inside would produce the correct result but still wouldn't allow a like comparison to match afterwards.

So now the following query

SELECT *,CONCAT_WS('',ExtractNumber_CHAR(`size`),`brand`,`pattern`,`product_code`,`size`,`normal_price`,`sale_price`,`text_special`,`load_index`,`speed_index`,`id`) `all_columns` 
FROM `baz_tyres` 
HAVING (`all_columns` LIKE '%nat%' )
ORDER BY `brand`  DESC
LIMIT 0,10

Produces the desired result however a NEW problem has now appeared that is really strange..

If I do the exact same query but order by ASC instead of DESC then I get 0 results.

Really strange that order affects weather any results are returned. If the extract number function is removed ordering either way returns results.

When I put the function back in I can only get results when order by desc.

Can anyone tell me why this odd behavior would occur?

使用 have 子句的语法是这个 SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY Being 在使用 group by 后与聚合函数一起使用 Try to change query by using group by else use where instead have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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