select on the basis of some specific value

Viewed 46

I have a input table in mysql

   colA   colB
   ------------
   ABC      -3
   DEF       2
   GHI      -1 
   ABC       4
   DEF      -2 
   GHI      -1
   JKL       2

and i want output as

   colA   colB
   ------------
   DEF       2
   GHI      -1 
   ABC       4
   DEF      -2 
   GHI      -1
   JKL       2

i only want to exclude the negative values for colB for colA = 'ABC' and want to keep rest all the negative and positive values for colA in ('DEF','GHI','JKL')

3 Answers

You may try the following

SELECT 
    colA,
    colB
FROM
    my_table
WHERE
   NOT (colA = 'ABC' and colB < 0)

View Demo Fiddle

Let me know if this works for you.

SELECT colA, colB
FROM table_name
WHERE colB>=0 OR colA IN ('DEF','GHI','JKL');

Second solution

SELECT colA, colB
FROM table_name
WHERE colB>=0 OR colA!='ABC';

One of the ways to do it is by using an OR condition.

SELECT * FROM input_table WHERE colA <> 'ABC' or colB > 0;

This query will return rows where any of the following conditions are true:

  • colA is different than "ABC"
  • colB is positive

If both conditions are false, it will not return that row.

Related