Jackbeeby Jackbeeby - 10 days ago 6
MySQL Question

cant have two where statements in mysql query

I have to search the database for certain results which are in one table but are not in a certain column in another table. my query below is what I have so far.

$sql = "SELECT * FROM users WHERE name LIKE '%".$_POST["search"]."%' AND NOT IN
(SELECT friend FROM friends WHERE user='{$user_name}')";


as you can see I have the WHERE and then I declare the first item it has to search for. But then I add in AND so I am can search another table which is in the brackets.

What is the best way I can do this action without having to change my format to much?

if anyone has any questions please post them below. So I can improve my question quality in the future please post below any ways that I can improve.

Answer

The problem isn't the use of two WHERE clauses, it's that instead of writing:

... WHERE name LIKE '%xxx%' AND NOT IN (...) ...

you have to write:

... WHERE name LIKE '%xxx%' AND name NOT IN (...) ...