Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <code>ids</code> is a field containing comma-delimited values, then your query is like:</p> <pre><code>SELECT * FROM `informations` WHERE 3 IN ("1,2,3,4,5") </code></pre> <p>Instead of what it should be:</p> <pre><code>SELECT * FROM `informations` WHERE 3 IN (1,2,3,4,5) </code></pre> <p>There is no automatic tokenisation (splitting on <code>,</code>) performed; the one value of <code>ids</code> is not automatically converted into a list for you such that <code>IN</code> can work.</p> <p>Unfortunately your table design has been your undoing here. Can you split the IDs into a separate table using the principle of <em>database normalisation</em>?</p> <p>Then your query might look like:</p> <pre><code>SELECT * FROM `informations` WHERE 3 IN ( SELECT `id` FROM `ids` WHERE `informations`.`id` = `ids`.`information_id` ) </code></pre> <p>BTW, "information" is a non-countable noun and, as such, "informations" is wrong.</p> <hr> <p><strong>Update</strong> (thanks for the idea, a1ex07!)</p> <p>Although this is hackery and I still suggest fixing your table layout, I'll be kind and suggest a quick fix.</p> <p>Willempie was close with:</p> <pre><code>$query = 'SELECT * FROM `informations` WHERE `ids` LIKE "%' . $id . '%"'; </code></pre> <p>Unfortunately, a wildcard match isn't quite powerful enough. Consider if <code>ids</code> is like <code>"1,6,9,12,35,4"</code> and <code>$id</code> is like <code>3</code>. You get a false positive. The <code>LIKE</code> statement needs to be aware of the commas.</p> <p>You can add multiple cases:</p> <pre><code>$query = 'SELECT * FROM `informations` WHERE `ids` LIKE "%,' . $id . ',%" OR `ids` LIKE "%,' . $id . '" OR `ids` LIKE "' . $id . ',%"'; </code></pre> <p>Or, for brevity, you can work around this with <a href="http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp" rel="nofollow">regular expressions</a>:</p> <pre><code>$query = 'SELECT * FROM `informations` WHERE `ids` REGEXP "(^|,)' . $id . '(,|$)"'; </code></pre> <p>For any <code>$id</code> you wish to find, before it must be the start of <code>ids</code> (<code>^</code>) or a comma; after it must be a comma or the end of <code>ids</code> (<code>$</code>). This ensures that <code>$id</code> must be found as a <em>whole, comma-delimited token</em>.</p> <p>It's a little like "Whole Word Only" in word processor searches, but with commas separating "words" instead of spaces.</p> <hr> <p><strong>Update 2</strong></p> <p>Another way uses <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set" rel="nofollow"><code>FIND_IN_SET</code></a>, which performs a search within a comma-delimited string:</p> <pre><code>$query = 'SELECT * FROM `informations` WHERE FIND_IN_SET("' . $id . '", `ids`)'; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload