samedi 31 janvier 2015

Is there any way to exclude results of a query based on its own results?

Say I have a normal SELECT call like (well, this is really simplified for the question):



SELECT * FROM table WHERE column_b = "X" and column_c = "Y"


but I also want to exclude the results where column_a (the primary key) matches column_k of any other matched results. I think I can do it something like (I haven't tested this because it's not the way I want to do it)



SELECT * FROM table WHERE column_b = "X" AND column_c = "Y"
AND column_a NOT IN
(SELECT column_k FROM table WHERE column_b = "X" AND column_c = "Y")


Being a big advocate of DRY programming, I don't like that this method pretty much repeats the entire SELECT call, if it even works. Is there a better way to do this?


Aucun commentaire:

Enregistrer un commentaire