I have two tables:
conversation_participants
id | conversation_id | user_id
int(11) | int(11) | int(11)
users
id | username | ....
int(11) | varchar(20)| ....
I am trying to select all the users which have a certain string in their username which are not (yet) participants in a particular conversation. Currently I have two SQL queries:
SELECT user_id FROM conversation_participants WHERE conversation_id=?
and
SELECT id, username from users WHERE username LIKE '%%%{$_GET['q']}%%' ORDER BY id DESC LIMIT 10
And remove all the results from the second query which have the same user_id as the ones from the first query. Is there any way to merge these two queries into one ?
Thank you in advance
Don’t build your sql statements concatenating strings. You should use some sort of utility method to sanitize your input. Your statement is prone to sql injection attacks.
PHP has several functions for this. Look into mysql_real_escape_string