我有一个问题,我不知道在mysql中有更好的方法。我在mysql中有一张表,上面有正则表达式的列表,每个正则表达式代表公司订单号
我希望能够将一个号码与该列表进行比较,以获取该号码所属的公司。懒惰的方法是在php中列出所有正则表达式,然后使用循环来获取公司,但是我想使用mysql的功能来做到这一点。
就像@Mech一样,这可能含糊不清。
我将尝试进一步解释:
我在“ ^ [8] {1} [0-9] {10} $”一列中有两个表,表中均带有实际正则表达式的纯文本格式
此模式属于一家公司,有500多个正则表达式模式。
谢谢。
答案 0 :(得分:1)
您在这里@ BM2ilabs。所要求的功能:)
carrierID(88888141234);
function carrierID($ordernum) {
// create a connection to your db here
// fetch data needed for loop
$sql = "SELECT regex, carrier_id FROM `company_tbl_from_image`";
// fetch results
$results = $conn->query($sql);
// loop through $results
foreach ($results as $result) {
// individually check against each regex in the table
$regex = $result[regex];
// find first instance of $regex, where the $ordernum is unique, there should only be one match
if (preg_match('/'.$regex.'/', $ordernum)) {
$carrier_id = $result[carrier_id];
break; // remove break to show other matches
}
}
// check if $carrier_id is empty
if ($carrier_id <> "") {
echo $carrier_id;
} else {
echo "No carrier ID found.";
}
}
答案 1 :(得分:0)
仅MySQL选项。只需搜索:
SELECT carrier_id FROM `company_tbl_from_image` WHERE 'order number' REGEXP regex