如何显示一个非空值然后显示所有空值

时间:2016-05-25 09:40:05

标签: mysql

您好我有一个mysql查询

SELECT 
    unicitem,
    unicreplacement,
    sc_products.productID,
    sc_products.brief_description_ru,
    sc_products.name_ru,
    price.postavchik as suupplier,
    sc_group_discounts.`action`,
    sc_group_discounts.procent,
    price.Price as Price,
    price.in_stock,
    price.supplier
FROM sc_products
LEFT OUTER JOIN t26_replaceable_items ON (sc_products.unic = t26_replaceable_items.unicreplacement)
LEFT OUTER JOIN sc_group_discounts ON (sc_products.item_group = sc_group_discounts.item_group)
LEFT JOIN price ON (t26_replaceable_items.unicreplacement = price.unic) AND (sc_products.suupplier=price.postavchik)     
WHERE t26_replaceable_items.unicitem = '0092S40050'
GROUP BY productID
ORDER by isnull (price.Price),price.Price ASC

我只想显示价格不为空的第一行,然后显示价格的所有空行。我不知道如何实现这个目标。

1 个答案:

答案 0 :(得分:0)

你的意思是这个,请试一试,希望我没有误会你的意愿;)

SELECT *
FROM (
    SELECT 
        unicitem,
        unicreplacement,
        sc_products.productID,
        sc_products.brief_description_ru,
        sc_products.name_ru,
        price.postavchik as suupplier,
        sc_group_discounts.`action`,
        sc_group_discounts.procent,
        price.Price as Price,
        price.in_stock,
        price.supplier
        , @rowno:=@rowno + 1 AS rowno
    FROM sc_products
    LEFT OUTER JOIN t26_replaceable_items ON (sc_products.unic = t26_replaceable_items.unicreplacement)
    LEFT OUTER JOIN sc_group_discounts ON (sc_products.item_group = sc_group_discounts.item_group)
    LEFT JOIN price ON (t26_replaceable_items.unicreplacement = price.unic) AND (sc_products.suupplier=price.postavchik)
    , (SELECT @rowno:=0) tmp   
    WHERE t26_replaceable_items.unicitem = '0092S40050'
    GROUP BY productID
    ORDER by isnull (price.Price),price.Price ASC
) t
WHERE rowno = 1 AND Price IS NOT NULL OR (rowno <> 1 AND Price IS NULL)

如果price全部为null,则此查询将不起作用。