I have to put down the lines that have two distinct fields with value = 0
Let me explain, all records that have stock_it=0 and stock_eu=0 must go to the bottom of the results
I have used this solution, however, it is not strictly correct because it puts down even rows that do not have both values = 0
$dati = mysql_query("
SELECT *
FROM $tb_article
ORDER BY
FIELD($tb_art.article_status,'n') DESC,
FIELD($tb_art.article_stock_it,'0') ASC,
FIELD($tb_art.article_stock_eu,'0') ASC,
$tb_art.article_rank
");
article_status=n indicates new products and they have to stay on top!You can use the CASE statement in ORDER BY, like this:
SELECT *
FROM $tb_article
ORDER BY
CASE WHEN article_status = 'n' THEN 0
WHEN article_stock_it = 0 THEN 100000
WHEN article_stock_eu = 0 THEN 100000
ELSE article_rank
END
If you want to use ORDER BY FIELD : SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE tb_article
(
article_stock_it int,
article_stock_eu int,
article_status varchar(11),
article_rank int
);
INSERT INTO tb_article VALUES
(1, 1, 'n', 1)
, (0, 1, 'n', 1)
, (0, 0, 'n', 1)
, (1, 1, 'o', 1)
, (1, 1, 'o', 2);
Query 1:
SELECT *
FROM tb_article
ORDER BY
FIELD(tb_article.article_status,'n') DESC,
FIELD(
IF(tb_article.article_stock_it = 0 AND tb_article.article_stock_eu = 0,'1','0')
,'0') DESC,
tb_article.article_rank
Results:
| article_stock_it | article_stock_eu | article_status | article_rank |
|------------------|------------------|----------------|--------------|
| 1 | 1 | n | 1 |
| 0 | 1 | n | 1 |
| 0 | 0 | n | 1 |
| 1 | 1 | o | 1 |
| 1 | 1 | o | 2 |
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