【问题标题】:MySQL Selecting Results using a wild card Not workingMySQL使用通配符选择结果不起作用
【发布时间】:2014-07-03 06:44:40
【问题描述】:

您好,我的表格数据如下所示

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)

我使用通配符选择从 b 或 p 开始的所有记录,但结果集为空,我在这里做错了什么,请帮助,提前谢谢。

mysql> SELECT * FROM user_details WHERE UserName LIKE  '[bp]%';
Empty set (0.00 sec)

【问题讨论】:

    标签: php mysql sql select wildcard


    【解决方案1】:

    另一种方法是使用Regular Expressions

    SELECT * FROM user_details WHERE UserName REGEXP '^[bp]';
    

    【讨论】:

    • 非常好的答案。谢谢。
    【解决方案2】:

    尝试删除括号并为bp 搜索标签添加单独的条件。

    SELECT * FROM user_details WHERE UserName LIKE  'b%' OR UserName LIKE 'p%';
    

    【讨论】:

    • 嗨,我试过这个教程,这对 mysql 不起作用吗w3schools.com/sql/sql_wildcards.asp
    • 你说得对,本教程解释了SQL查询,它不适用于mysql
    • @Sadikhasan he he,我永远不会忘记,但仍然无法接受答案,我也给你投票了:p
    【解决方案3】:

    使用这样的代码,

    mysql> SELECT * FROM user_details WHERE (UserName LIKE  'b%') OR (UserName LIKE 'p%');
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多