mysql 中casewhen用法


需求:需要在查詢出的字段中,判斷這個某個字段是否為null,並指定想要的值,

 

SELECT
    temp.gId id,
    temp.`name`,
    temp.point,
    temp.give_user_proportion,
    temp.sales_price,
    temp.cover_img,
    temp.limit_num,
    temp.price,
    temp.show_sale_price,
    temp.isPromotion is_promotion
FROM
    (
        SELECT
            allgoods.gId,
            allgoods.`name`,
            allgoods.sale_num,
            allgoods.point,
            allgoods.give_user_proportion,
            allgoods.sales_price,
            allgoods.cover_img,
            allgoods.modify_date,
            allgoods.productId,
            pi.limit_num,
            pi.price,
            pi.show_sale_price,
            CASE
        WHEN pi.price IS NULL THEN
            FALSE
        ELSE
            TRUE
        END AS isPromotion
        FROM
            (
                SELECT
                    good.id gId,
                    good.`name`,
                    good.sale_num,
                    good.point,
                    good.give_user_proportion,
                    produc.sales_price,
                    produc.cover_img,
                    produc.modify_date,
                    produc.id productId
                FROM
                    (
                        SELECT
                            *
                        FROM
                            goods goodstemp
                        WHERE
                            goodstemp.is_del = FALSE
                        AND goodstemp.check_status = 'configed'
                        AND goodstemp.category_id = 174976742484877312
                    ) good
                LEFT JOIN (
                    SELECT
                        *
                    FROM
                        product p2
                    WHERE
                        p2.is_del = FALSE
                    AND p2.is_putaway = TRUE
                    AND p2.is_show = TRUE
                ) produc ON good.id = produc.goods_id
                WHERE
                    good.id = produc.goods_id
            ) allgoods
        LEFT JOIN (
            SELECT
                *
            FROM
                promotion prom
            WHERE
                prom.is_del = FALSE
            AND TO_DAYS(NOW()) >= TO_DAYS(prom.start_date)
            AND TO_DAYS(NOW()) <= TO_DAYS(prom.end_date)
            AND prom.publish_status = 'published'
        ) promotiontemp ON allgoods.gId = promotiontemp.goods_id
        LEFT JOIN promotion_item pi ON promotiontemp.id = pi.promotion_id
    ) temp
ORDER BY
    temp.isPromotion DESC

結果:

 

 

當你的表示myisam時:

SELECT * FROM tbl -- this will do a "table scan". If the table has never had any DELETEs/REPLACEs/UPDATEs, the records will happen to be in the insertion order, hence what you observed.
大致意思為,一個myisam引擎表在沒有任何的刪除,修改操作下,執行 select 不帶order by,那么會按照插入順序進行排序。

If you had done the same statement with an InnoDB table, they would have been delivered in PRIMARY KEY order, not INSERT order. Again, this is an artifact of the underlying implementation, not something to depend on.
對於innodb引擎表來說,在相同的情況下,select 不帶order by,會根據主鍵來排序,從小到大


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM