douxing1969
douxing1969
2014-07-03 06:44

MySQL使用通配符选择结果不工作

已采纳

Hi my table data look like this

mysql> SELECT * FROM user_details
    -> ;
+----+----------+------+-----+
| ID | UserName | Type | Age |
+----+----------+------+-----+
|  2 | bandara  |    2 |  26 |
|  3 | panama   |    1 |  12 |
|  4 | sunil    |    2 | 100 |
|  5 | aaa      |    1 |  50 |
+----+----------+------+-----+
4 rows in set (0.00 sec)

iI used a wildcard to select all recods starting from either b or p , but the result set is empty , what i am doing wrong here , please help , thanks in advance.

mysql> SELECT * FROM user_details WHERE UserName LIKE  '[bp]%';
Empty set (0.00 sec)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douzao9845 douzao9845 7年前

    Another way is to use Regular Expressions :

    SELECT * FROM user_details WHERE UserName REGEXP '^[bp]';
    
    点赞 评论 复制链接分享
  • doo6568 doo6568 7年前

    Use code like this,

    mysql> SELECT * FROM user_details WHERE (UserName LIKE  'b%') OR (UserName LIKE 'p%');
    
    点赞 评论 复制链接分享
  • douxia6554 douxia6554 7年前

    Try to remove bracket and put individual condition for b and p search tag like.

    SELECT * FROM user_details WHERE UserName LIKE  'b%' OR UserName LIKE 'p%';
    
    点赞 评论 复制链接分享