Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude rows with specific columns values from SELECT

Tags:

sql

mysql

I've got table with following columns and data:

stock   quant
-----   -----
10      0
10     -5
10      1
1       20
10      1
10      88
5       1

What I need is to exclude rows with stock_status = 10 and quantity <= 0 at the same time.

I need to have those rows with stock_status = 10 but quantity > 0 or the other way around.

So the desire output from this would be

stock   quant
----    ---
10      1
1       20
10      1
10      88
5       1

Thanks.

like image 963
JTC Avatar asked Jan 21 '26 10:01

JTC


2 Answers

Well you actually wrote the query yourself by telling us what you need to exclude...

SELECT stock, quant
FROM yourTable 
WHERE NOT(stock_status = 10 AND quantity <= 0); 

You should follow a tutorial on SQL query (for example on W3school) as this is very basic and you should be able to do a query like that in less than a minute after following a very short tutorial for beginner.

I recommend this link : SQL Tutorial.

like image 154
Jean-François Savard Avatar answered Jan 24 '26 09:01

Jean-François Savard


SELECT stock, quant
FROM yourTable
WHERE NOT (stock_status = 10 AND quantity <= 0)

or, apply de Morgan's Laws:

SELECT stock, quant
FROM yourTable
WHERE stock_status != 10 OR quantity > 0
like image 25
Barmar Avatar answered Jan 24 '26 09:01

Barmar