Click here to Skip to main content
15,896,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ID       RoomNo

1     4, 5, 6, 7, 8
2     4, 5, 6
3     6, 7, 8
4     5, 6
5     4, 5, 6, 7
6     4, 15, 6, 7


'RoomNo' field type is varchar.

When I use
SQL
SELECT * FROM table WHERE RoomNo LIKE '%5%' ;

am getting value but thats not proper because i need value contain 5 but it will get 15 also in id - 6

and also i tried to use this query
SQL
SELECT * FROM table WHERE FIND_IN_SET(7, RoomNo);


but it not showing result
Posted

1 solution

The problem is your data. %5% literally means <zero or more characters>5<zero or more characters>.
You could edit the first query to exclude results that are not valid such as;
SQL
SELECT * FROM table WHERE RoomNo LIKE '%5%' and RoomNo NOT LIKE '%15%'

But this is inefficient and would require you to enter every other value that you do not want but still includes a 5. The number of exclusions is determined by the number of possible characters, if you are talking about numbers only then you would have 20 - see below;
05, 15, 25, 35, 45, 55, 65, 75, 85, 95
50, 51, 52, 53, 54, 55, 56, 57, 58, 59
If you also had the possibility of characters a-z then you would have 52 exceptions.

Additionally my understanding of the function FIND_IN_SET does not work the way you are using it. It returns the position in a string, not the position in a table - refer to the below link for documentation.
http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900