MySQL LIKE with range doesn't work

前端 未结 4 2067
余生分开走
余生分开走 2020-12-07 03:12

I\'ve got a database table mytable with a column name in Varchar format, and column date with Datetime values. I\'d like to count names with certa

4条回答
  •  孤城傲影
    2020-12-07 03:57

    I believe LIKE is just for searching for parts of a string, but it sounds like you want to implement a regular expression to search for a range.

    In that case, use REGEXP instead. For example (simplified):

    SELECT * FROM mytable WHERE name REGEXP "[a-z]"
    

    Your current query is looking for a string of literally "[a-z]".

    Updated:

    SELECT
    CAST(t.date AS DATE) AS 'date',
    COUNT(*) AS total,
    SUM(LENGTH(LTRIM(RTRIM(t.name))) > 4 
        AND (LOWER(t.name) REGEXP '%[a-z]%')) AS 'n'
    FROM
    mytable t
    GROUP BY 
    CAST(t.date AS DATE)
    

提交回复
热议问题