按问题排序的SQL - 不能按指定的列排序

时间:2013-01-07 13:52:15

标签: php mysql sql

我不是SQL方面的专家,因此需要你的帮助。

我有其他人写的高级查询:

SELECT cms_product_bid.*,
    cms_products.*,
    cms_products_images.pd_image, 
    cms_products_images.pd_image_label,
    IF (cms_product_bid.pd_bid = 1,
        IF(cms_product_bid.pd_bid_start_date <= CURDATE(),
            IF(cms_product_bid.pd_bid_end_date >= CURDATE(),1,0)
        ,0)
    ,1)  AS bid_product_check
FROM cms_products
LEFT JOIN cms_products_images 
ON (cms_products.pd_id = cms_products_images.pd_id)
INNER JOIN cms_product_bid 
ON (cms_product_bid.product_id = cms_products.pd_id)
INNER JOIN cms_home_product_slider 
ON (cms_products.pd_id = cms_home_product_slider.pd_id)
WHERE cms_products.pd_status=1 
    AND cms_products.pd_visibility=1 
    AND cms_home_product_slider.cat_id='$featured_cat_id'
GROUP BY cms_products.pd_id 
HAVING(bid_product_check =1)
ORDER BY cms_products.pd_sort ASC

我要做的就是按cms_products.pd_sort进行排序,但即使表格中的列包含12和{{1}}等值,也不按列排序等等。

有人能指出这个问题吗?

2 个答案:

答案 0 :(得分:2)

我的猜测是,您的列的类型为VARCHAR字符串,您只需CAST

SELECT...
FROM...
WHERE...
ORDER BY CAST(cms_products.pd_sort AS SIGNED) ASC

答案 1 :(得分:1)

它可能是一个错误...但是尝试列出列而不是使用。*快捷方式...然后查看该列是否会被正确识别和排序。

顺便说一下,使用。*不是最佳做法,原因很多。