简体   繁体   中英

mysql select min price value exclude zero

I need to fetch the minimum price value and exclude zero values from database. Here is mysql query that is not working:

SELECT MIN(price) AS lowprice FROM price WHERE status = 1 AND price > 1 LIMIT 1

I tried this on my own database, and used:

SELECT MIN(price) AS lowprice 
FROM price 
WHERE status = 1 AND price > 0

This gave me all the values where status=1 and where price > 0. So the only difference was price > 0 instead of price > 1 . But if you want to also exclude values of 1, then price > 1 works too.

Update:

As Juan Carlos Oropeza said in comments, you don't need the LIMIT 1 . I left it out of my code, but didn't mention this.

In MySQL you can use the NULLIF function.

SELECT MIN(NULLIF(price, 0)) AS lowprice FROM price WHERE status=1

NULLIF will compare the price value with 0 and if it's true, it will return null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM