【问题标题】:searching alphanumeric string搜索字母数字字符串
【发布时间】:2013-06-13 07:19:25
【问题描述】:

我在 mysql 表中有一个 varchar 字段,我想在 php 中搜索字母数字字符串 现在它只显示数字结果。

例如,如果我搜索 111,那么它应该会显示匹配 111(111A, 111B, 111C) 的结果。如果我搜索 111A,那么它应该会显示 111A 的结果。 如何做到这一点。

【问题讨论】:

  • 像'%111%'这样的列

标签: php mysql query-string alphanumeric


【解决方案1】:

您的情况似乎与 LIKE 的做法相反。看起来您已经使用了 LIKE,并且不应该将它用于任何精确匹配。查询可能看起来像

SELECT myField from MyTable where myValue = '111'
SELECT myField from MyTable where myValue = '111A'

达到相反的效果

你可以使用LIKE

SELECT myField from MyTable where myValue LIKE '111%'

这适用于像 111Anything 这样的字符串

SELECT myField from MyTable where myValue LIKE '%111%'

这适用于像 Anything111Anything 这样的字符串

SELECT myField from MyTable where myValue LIKE '%111'

这适用于像 Anything111 这样的字符串

【讨论】:

  • 谢谢。 ( SELECT myField from MyTable where myValue = '111') 工作。
【解决方案2】:

php 中的搜索模式: 来自http://www.php.net/manual/en/function.preg-grep.php

<?php
$subject = array("111A", "111B", "222");
$pattern = "/^111/";
$matches = preg_grep($pattern, $subject);
print_r($matches);
?>

【讨论】:

    【解决方案3】:

    你可以使用REGEXP代替LIKE

    对于从 111 开始

    SELECT myField from MyTable where myValue REGEXP `^111`;
    

    对于以 111 结尾

    SELECT myField from MyTable where myValue REGEXP `111$`;
    

    对于以 111 开始,以 111 结束

    SELECT myField from MyTable where myValue REGEXP `^111$`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多