2

I got two tables :

  • A list of people;
  • A list of people I want to ignore.

When I read the list of people, I don't want to see the ignored people in the list.

My current solution is to query a second time the database (to select the people I want to ignore) and remove them from the array I create with PHP. It's working and it's fine.

However, I want to do that in MySQL. I know JOIN will join only if the row exists in the other table. I am looking for something different (won't show the entry IF the row exists).

I have searched in Google but the lack of "keywords" for this gave me no results.

Thanks

Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
David Bélanger
  • 7,400
  • 4
  • 37
  • 55

3 Answers3

7
SELECT * FROM Person
LEFT OUTER JOIN IgnoredPerson
ON Person.id = IgnoredPerson.id
WHERE IgnoredPerson.id IS null

Explanation:

enter image description here

Exclude the records we don't want from the right side via a where clause

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Without knowing your schema, I'd suggest something along these lines:

SELECT * FROM people WHERE id NOT IN (SELECT person_id FROM ignored_people)
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
2

You could try something like this

 SELECT * FROM people p WHERE NOT EXISTS (SELECT i.id FROM ignorePeople i where p.id = i.id )

here's a link about EXISTS in MySql

Marc
  • 16,170
  • 20
  • 76
  • 119