开发者

MySQL query for alphabetical search

How to retrieve the records those character star starting with A OR B OR C ,

I just tried t开发者_如何学运维his query

SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) = 'a'

 ---> display all customer name starting with character a

AND I added one more condition, That is

SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) = 'a' or Left(cus_name, 1) = 'b'

 --->It  displayed all customer name starting with character a and in some records middle i saw some name starting with b also, 

What i want is , i want pull out records , which names are starting with A or B or C ,

And also it should order by alphabetical order,

Also i tried this below query.

SELECT * FROM `test_tbl` WHERE Left(cus_name, 1) REGEXP '^[^a-z]+$';

The rendered records for that above is , just two records started with A,

This above question for doing the alphabetical pagination ,

Thanks


You can try:

SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';

It will list all rows where cus_name starts with either a, b or c.

The regex used is ^[abc]:

  • ^ : Is the start anchor
  • [..] : Is the character class. So [abc] matches either an a or a b or a c. It is equivalent to (a|b|c)

The regex you were using : ^[^a-z]+$

  • The first ^ is the start anchor.
  • [..] is character class.
  • The ^ inside the character class negates it. So [^abc] is any character other than the three listed.
  • + is the quantifier for one or more.
  • $ is the end anchor.

So effectively you are saying: give me all the rows where cus_name contains one or more letters which cannot be any of the lowercase letters.


try like instead

SELECT * FROM `test_tbl` WHERE cus_name like 'a%'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜