-1

I am constructing an SQL statement with some parameters. Finally, an SQL statement is created like "select * from table where column1 = "xyz"".

But I also need the rows which are filtered with this statement. In this case they're rows which are not "xyz" valued in column1. More specifically, I am looking for something like INVERSE(select * from table where ...). Is it possible?

Edit: My bad, I know I can do it with != or operator. Here the case is, select statement may be more complex (with some ANDs and equal, greater operators). Let's assume a table has A,B,C and my SQL statement brings only A as result. But I need B and C while I only have the statement which brings A.

ugur
  • 824
  • 1
  • 7
  • 15

5 Answers5

1

select * from table where column1 != 'xyz' or column1 is null;

Xophmeister
  • 8,884
  • 4
  • 44
  • 87
0

If you want the other ones, do it like this:

select * from table where column1 <> "xyz"

column1 <> (differs from) "xyz"

aF.
  • 64,980
  • 43
  • 135
  • 198
0

To check if something is no equal you can use <> or even !=

SELECT *
FROM yourTable
WHERE <> 'xyz'

OR

SELECT *
FROM yourTable
WHERE != 'xyz'

Many database vendors support (see list) both versions of the syntax.

Community
  • 1
  • 1
Taryn
  • 242,637
  • 56
  • 362
  • 405
0

If you're retrieving both result sets at about the same time, and just want to process the xyz ones first, you could do:

select *,CASE WHEN column1 = "xyz" THEN 1 ELSE 0 END as xyz from table
order by CASE WHEN column1 = "xyz" THEN 1 ELSE 0 END desc

This will return all of the rows in one result set. Whilst xyz = 1, these were the rows with column1 = 'xyz'.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
0

It was :

"select * from table where rowId NOT IN (select rowId from table where column1 = "xyz")

I needed a unique rowId column to achieve this.

ugur
  • 824
  • 1
  • 7
  • 15