0

I have an SQL table and I need to select all the records where username (MM_Session) is in another users following list (Users SQL table, following column), which would look like this

username1 username2 username3 username4

So far, my SQL query looks like this:

SELECT *
FROM Users
WHERE Users.Following_By LIKE '" . $_SESSION['MM_Username'] . "'

Except that doesn't seem to return any values even though the user's username is in the other users following list.

Should I be using 'IN' instead of 'LIKE'?

Does anyone have any other ideas why this isn't working?

Thanks

1
  • 1
    Which DBMS are you using? And what programming language is that? Commented May 29, 2016 at 6:50

1 Answer 1

2

The LIKE operator works just the same as = if you don't use wildcard characters in your query string. Place them before and behind the word to match any occurrence of the word:

SELECT *
 FROM Users
 WHERE Users.Following_By LIKE '%" . $_SESSION['MM_Username'] . "%'

Though you might get more users than you expected because it matches for example foobar if MM_USERname contains only foo. You could use explicit delimiters to prevent this. Or, even better, use a separate table for the list entries and use a foreign key to join it to the Users table.

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

2 Comments

Thank you. I don't really know why I didn't think of this before. It's such an obvious answer.
Yes, I did try earlier but it said I had to wait 10 minutes. It's done now.

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.