Answer the question
In order to leave comments, you need to log in
How to output all null values separately from general sort in mySQL?
In the storehouses_products inventory table
, the value field can contain a variety of numbers: 0 if the product is out of stock and above zero
if there are stocks in the warehouse. You need to sort the entries so that they appear
in ascending order of value. Zero inventory should be output at the end, after all entries.
DROP TABLE IF EXISTS storehouses_products;
CREATE TABLE storehouses_products (
id SERIAL PRIMARY KEY,
storehouse_id int unsigned,
product_id int unsigned,
value int unsigned comment 'Запас товарной позиции на складе',
created_at DATETIME default current_timestamp COMMENT 'Дата регистрации',
updated_at DATETIME default current_timestamp ON UPDATE current_timestamp COMMENT 'Дата последнего обновления записи'
) COMMENT = 'Склады и продукты';
INSERT INTO storehouses_products (storehouse_id, product_id, value) VALUES
(1, 1, 0),
(1, 2, 4),
(1, 3, 0),
(2, 4, 7),
(2, 5, 56),
(1, 6, 0),
(1, 7, 4);
SELECT * FROM storehouses_products;
Answer the question
In order to leave comments, you need to log in
SELECT * FROM storehouses_products
ORDER BY CASE WHEN value = 0 THEN 2147483647 ELSE value END
SELECT 0 AS line, * FROM storehouses_products WHERE value > 0
UNION
SELECT 1 AS line, * FROM storehouses_products WHERE value < 1
ORDER BY line, value
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question