Thursday, 12 September 2013

MySQL combing results

MySQL combing results

I have a table users and I have a simple search query:
SELECT *
FROM users
WHERE username LIKE 'rya%'
LIMIT 10
I also have another table called follows which basically is a list of
users being followed by other users. I modified the above query to search
only within what particular user is following:
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
Most of the times, the query above only returns couple of results (because
I am not search the entire users table).
What I want to do is combine the above 2 queries, to return a MAXIMUM of
10 results of usernames matching a certain string with first listing the
user's following, and then search the entire table of users.
I already have a solution but it requires doing this at application level
by first running the 2nd query, then first, and coming the two. Is there a
way I can do all of this in 1 query?
Thanks

No comments:

Post a Comment