douzhang2092 2016-12-26 06:30
浏览 51
已采纳

Mysql where子句(搜索表列匹配)[重复]

This question already has an answer here:

I have 1 table, t1, around 500+ data row, I just show a sample data.

Data as below:

+--------+----------+-------------------+
| id     | Name     | category          |
+--------+----------+-------------------+
| 1      | ABC      | 6,9,25,27         |
+---------------------------------------+

My mysql query like below:

$gcategory = intval($_GET['cat']);
$test = DB::fetch_all("SELECT * FROM t1 WHERE category like '%$gcategory%' ORDER BY id DESC");
foreach($test as $te){
    $list[] = $te;
}

But if $gcategory = '7'; the ABC also will appear in my $list[], but I just want when $gcategory = '6' || $gcategory = '9' || $gcategory = '25' || $gcategory = '27' then ABC only appear in my $list[]? how to fix this?

Thanks.

</div>
  • 写回答

2条回答 默认 最新

  • dongmoxin7111 2016-12-26 06:35
    关注

    Please try like following way, you should use find_in_set php function when you finding from , seperated list of value:

    $gcategory = intval($_GET['cat']);
    $test = DB::fetch_all("SELECT * FROM t1 WHERE FIND_IN_SET("'.$gcategory.'", category) ORDER BY id DESC");
    foreach($test as $te){
        $list[] = $te;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?