php - 在PHP中使用正则表达式查询MYSQL

标签 php mysql regex

我正在尝试使用正则表达式查询 MYSQL 数据库。我正在寻找具有此模式 1234X32X12 的列下的值。

我写了一些伪代码,但我不熟悉正则表达式,所以我希望有人能帮助我。

代码:

if ($transposed_array[$i][0] starts with number)
    {
        $sid = regex something /^[0-9]*/
        $gid = regex something /X[0-9]*X/
        $gid = remove first and last character from $gid
        $qid = regex something /[0-9]*$/

        $question_text = mysql_query("select question from table where sid = ".$sid." and gid = ".$gid." and qid = ".$qid." limit 1");
        $transposed_array[$i][0] = $question_test
    }

如有任何帮助,我们将不胜感激!

最佳答案

MySQL 在其 SQL 命令中直接支持正则表达式匹配,查看 its manual :

select col from table where col regexp '^h';

( see this at sqlfiddle )

关于php - 在PHP中使用正则表达式查询MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075058/

相关文章:

python - 忽略列表中包含括号的单词的最有效(Pythonic)方法是什么?

javascript - 使用正则表达式删除 HTML 标签和换行符

javascript - 捕获后面没有符号的单词

php - 由于在 php 中的 htmlspecialchars 和 htmlentities 中使用单引号而导致错误

javascript - 我如何使用 PHP 对包含引号的字符串进行编码以使其对于内联 'onclick' 是安全的?

MySQL FIND_IN_SET() 未按预期工作

mysql - 尝试通过 csv 文件更新数据库中的行

php - 在网格中显示表格中的图像

php - 我的数据库查询一直在 MySQL 中将所有列设置为相同的值

mysql - 从 ruby​​ on Rails 中的 Controller 运行 mysql 查询