Find specific value in comma list in database

前端 未结 2 460
傲寒
傲寒 2021-01-28 20:04

In one of my tables, i have a column where it has a comma seperated list of users that have bought that item. IMAGE

How can i read the list and run a php script to find

相关标签:
2条回答
  • 2021-01-28 20:43

    Use FIND_IN_SET() to find the records that contain your user

    select *
    from your_table
    where find_in_set('MTNOfficial', usersBought) > 0
    
    0 讨论(0)
  • 2021-01-28 20:56

    Get the comma separated list, then try the php function explode() to get an array that you can loop over.

    http://php.net/manual/en/function.explode.php

    $target = "MTNOfficial";
    $query = "select usersBought from table_name";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $users = explode("," $row['usersBought']);
        // may want to trim() here
        for ($i = 0; $i < count($users); i++) {
            if ($users[i] == $target) {
                //we found him!
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题