简体   繁体   English

MySQL正则表达式与数组

[英]Mysql Regexp with array

I am getting an array of category id eg. 我正在获取类别ID的数组,例如。 ['1','2','3'] and in my database table it is stored as 1_abc eg : category : 1_Sameer Now it is easy if i get one value ['1','2','3']并在我的数据库表中存储为1_abc例如:category:1_Sameer现在,如果我得到一个值就很容易

ie. WHERE category like 1_%;

But since this is an array how can i use like or regex to compare with the database values 但是由于这是一个数组,我该如何使用like或regex与数据库值进行比较

In this case you can do as many queries as you have array elements and merge these results together if needed to get single collection of elements. 在这种情况下,您可以进行与数组元素一样多的查询,并在需要时将这些结果合并在一起以获得元素的单个集合。

$results = new Collection();
foreach($ids as $id) {
$categories=Category::where("id","like","$id_%")->get();
    $results=$results->merge($categories);
}

you have to do looping for that 你必须为此做循环

for($i=0;$i<=count($array_result)-1;$i++) { for($ i = 0; $ i <= count($ array_result)-1; $ i ++){

$result=mysql_query("select * from table_name where category like '{$array_result[$i]}_%' "); $ result = mysql_query(“从表名中选择*,其中类别类似于'{$ array_result [$ i]} _%'”);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM