简体   繁体   中英

PHP MySQL select where $id is in string

how could I select the values from the table where my passed ID value is inside a string? Values of columns can be 3326,9023,3725,552,1559 or 3326 , it's a string of n values. I need to get the row if my passed ID is inside this string.

Query:

$result = $this->db->query("
        SELECT b.*
        FROM `values` AS `b`
        WHERE b.ids = ?
    ", array($id))->row_array();

If the list of ids is in the column values, you can use FIND_IN_SET to search for your passed id:

$result = $this->db->query("
        SELECT b.*
        FROM `values` AS `b`
        WHERE FIND_IN_SET(?, b.ids)
    ", array($id))->row_array();

use in if you want use list of ids

SELECT b.*
        FROM `values` AS `b`
        WHERE b.ids in (3326,9023,3725,552,1559,3326) // your list of ids

but if value is string then use FIND_IN_SET

 WHERE FIND_IN_SET(table.id, commaSeparatedstringvalue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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