0
id | name | permission
1  | John | 1,4,3
2  | mike | 7,4,3
3  | sky  | 3,2,1

this is my database

now i have fetch select query with where condition

e.g. select * from friend where permission='4'

but i m not able fetch any data so what to do ?

Please help

4
  • 2
    Don't store the permission values like that, it will only cause you a lot of problems. Have one row for each permission! And, also, use correct data types, integer in this case! Commented Feb 20, 2015 at 10:41
  • find_in_set or something like that for mysql query. Commented Feb 20, 2015 at 10:41
  • 2
    Normalization is for this purpose! store different permissions in different rows Commented Feb 20, 2015 at 10:42
  • anyways is the permission tab a list or normal string? Commented Feb 20, 2015 at 10:43

3 Answers 3

2

Select * from friend where FIND_IN_SET('4',permission);

Sign up to request clarification or add additional context in comments.

1 Comment

Accept it as answer if you finding its working for you. Thanks!
2

It looks like you have multiple permissions stored together in a string.

To you, the query looks like:

select * from friend where permission='4'

That means anything containing 4. To mysql, it sees:

select * from friend where permission= ONLY '4'
// Meaning the permissions column can ONLY CONTAIN 4.
// Also, ONLY is meant as a visual, don't use it in queries.

Try:

find_in_set('4',permission) <> 0
// This means It needs to find number 4 and can't return 0 results.

Check out these for more info:

MySQL query finding values in a comma separated string

http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php

Comments

0

It can be done by the following query :

select * from friend where permission like '%4%'

1 Comment

This is a bad example as it will allow anything with 4. So if for some reason there was a permission 4 and 14, it would allow both, even if they only wanted just 4. This is because it just finds something with 4 in it from the way you wrote it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.