0

hello i have table with the column name id,receiver_id and many more now i want to fetch record in which sender_id is current_logged in user and receiver_id contains the current loggend in id.

means receiver_id contains the string of user_id separated with , now i want to check whether user_id there in receiver_id or not

suppose my id is U112 and receiver_id contains string U112,U123,U157 now how can i check

i tried as follow but fails

$selDoc="SELECT * FROM documents WHERE sender_id='U112' OR 'U112' IN (receiver_id) ORDER BY id DESC";

2
  • you should probably normalise your schema.. this is going to be a full table scan Commented Apr 23, 2013 at 10:42
  • i cant change the field now i have to work with it only Commented Apr 23, 2013 at 10:43

4 Answers 4

3

str IN (str2) is the same as str == str2.

Use FIND_IN_SET:

FIND_IN_SET('U112', receiver_id)

Note: you should probably normalize your schema.. this is going to be a full table scan.

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

3 Comments

i cant change database because it already live and running on server
well, at some point you have to learn how to update a production database.. schemas aren't written in stone ;)
but i am not authorized to do that
0

or you can use

receiver_id LIKE "% U112 %"

1 Comment

i thought that it is maybe fixed with 3 numbers after letter, sorry my bad :(
0

This might work correctly:

SELECT * FROM documents WHERE sender_id in ('U112','U113','U113')

OR

SELECT * FROM documents WHERE sender_id ='U112' and sender_id in ('U112','U113','U113')

Note: I worked on my own table and it worked perfectly. Example:

select * from tbl_answers where ques_id in ('69','12','14')
select * from tbl_answers where ques_id = 69 and ques_id in ('69','12','14')

Comments

0

You can try like this:

SELECT  * FROM documents WHERE (receiver_id LIKE '%,U112' OR receiver_id LIKE 'U112,%' OR receiver_id LIKE '%,U112,%' OR receiver_id LIKE 'U112')

Comments

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.