find_in_set mysql query

By | September 15, 2012

In some cases a field assigned for a user may be more and these are stored in a single column by using comma separation between each number. If we want to check whether any single number present in that column, we have to use in_array() php function.

Note: find_in_Set mysql query is equivalent for php in_array() function

Table Name: tb2

 

find_in_set mysql query is used to search a number is present in a column (where the numbers in this column are seperated by commas)

For example: In first row under category, if we want to check whether 2 is present in 2,3 we have to use mysql query find_in_set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
<body>
<?php
$query=mysql_connect('localhost','root','');
mysql_select_db("freeze",$query);

$category=array(1,2,3)
$query1=mysql_query("select * from tb2 where find_in_set(2,$category)");
while($query2=mysql_fetch_array($query1))
{
echo $query2['name'];
}
?>
</body>
</html>

 

OUTPUT:

prasad

Newbie

3 thoughts on “find_in_set mysql query

  1. minniie

    hi Prasad,

    I’m trying to execute this:
    $query1=mysql_query(“SELECT id=’$id’ FROM products WHERE FIND_IN_SET (’8′, size)”); while($query2=mysql_fetch_array($query1)) echo $query2['size'];
    }
    but code is not return any values.

    However when id=’$id’ is replaced with an * it returns all the sizes in my db when I only want to echo one for that product id.

    PLEASE HELP ASAP…THANKS IN ADVANCE!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

reductive-referenced