For this query
SELECT min(date) min_date FROM order_products group by order_id
min_date
ignores the NULL
values in a group of order id.
In my case I want min_date = NULL if the group has a order_products tuple with null value in date.
Any idea how can I achieve this ?
The MIN function works with numeric values and ignores NULL values.
By using the assignment operator (“=”), you can set any value of a column to NULL by using the Update Statement.
To enforce NOT NULL for a column in MySQL, you use the ALTER TABLE .... MODIFY command and restate the column definition, adding the NOT NULL attribute.
By default the functions MAX and MIN do not count NULL in their evaluation of your data. If we have a column containing only dates for instance and there is a NULL date, MAX and MIN will both ignore that value.
A simple workAround could be:
SELECT min(IFNULL(date, '1970-01-01')) AS min_date
FROM order_products
GROUP BY order_id
So, if 1970/01/01 turns up, you know that there's a NULL value.
select case when count(date)=sum(1) then min(date) else null end
or, when all rows are not null, return min date, else return null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With