Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select min returns empty row

Query

select min Price, min year from tblProduct

returns empty ("") results. I only want results in numbers. How do I do that?

Example:

tblProduct
Price   Year

1000    2008
2000    2009
500  2001
        2005
200  2000

The result is Price "", year 2000. I want the result to be Price 200, year 2000

like image 244
Nicklas Avatar asked Jan 26 '26 05:01

Nicklas


2 Answers

First of all, what is your RDBMS? For the ones I tried (MySQL and PostgreSQL), MIN ignores NULL.

Therefore, I assume your column is of type varchar or text. Which doesn't make sense for a price, so I suggest that you change it to a numeric type.

If you don't want to, then I'd use something like this :

SELECT MIN(price), MIN(year)
FROM tblProduct
WHERE price <> ''

Which would return (200, 2000). But really, it doesn't make sense. You want to use a numeric type and you want to use NULL instead of using an empty string.

like image 151
Vincent Savard Avatar answered Jan 28 '26 00:01

Vincent Savard


Try this one

SELECT     MIN(Price) AS Price, MIN(Year) AS Year
FROM         tblProduct
HAVING      (MIN(Price) IS NOT NULL) OR
                      (MIN(Price) <> '')
like image 37
Raymund Avatar answered Jan 28 '26 00:01

Raymund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!