MySQL - If It Starts With A Number Or Special Character

前端 未结 3 2099
独厮守ぢ
独厮守ぢ 2020-12-15 01:21
SELECT * 
FROM `thread` 
WHERE forumid NOT IN (1,2,3) AND IF( LEFT( title, 1) = \'#\', 1, 0)
ORDER BY title ASC

I have this query which will select

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 01:59

    If you want to select all the rows whose "title" does not begin with a letter, use REGEXP:

      SELECT * 
        FROM thread 
       WHERE forumid NOT IN (1,2,3)
         AND title NOT REGEXP '^[[:alpha:]]'
    ORDER BY title ASC
    
    • NOT means "not" (obviously ;))
    • ^ means "starts with"
    • [[:alpha:]] means "alphabetic characters only"

    Find more about REGEXP in MySQL's manual.

提交回复
热议问题