2016-05-25 62 views
1

你好,我有一個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 

我只是想表明第一行是那些價格不爲空,然後將所有的價格空行。我不知道如何實現這一點。

回答

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:[email protected] + 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) 

如果價格是所有空,這個查詢將無法工作。

相關問題