MySQL - 如果它以数字或特殊字符开头

2021-12-19 00:00:00 select mysql

SELECT *从`线程`论坛 ID 不在 (1,2,3) 和 IF( LEFT( title, 1) = '#', 1, 0) 中按标题 ASC 排序

我有这个查询,如果它以# 开头,它将选择某些内容.我想要做的是,如果 # 作为一个值给出,它将查找数字和特殊字符.或者任何不是普通字母的东西.

我该怎么做?

解决方案

如果要选择所有标题"不以字母开头的行,请使用 REGEXP:

 SELECT *从线程论坛 ID 不在的地方 (1,2,3)和标题不是正则表达式 '^[[[:alpha:]]'按标题 ASC 排序

  • NOT 的意思是不"(显然 ;))
  • ^ 表示以"开头
  • [[:alpha:]] 表示仅限字母字符"

在 MySQL 手册中查找有关 REGEXP 的更多信息.>

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 something if it starts with a #. What I want to do is if # is given as a value it will look for numbers and special characters. Or anything that is not a normal letter.

How would I do this?

解决方案

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.

相关文章