MySQL按组排序

时间:2015-05-28 12:13:14

标签: mysql sql group-by zen-cart

我知道Stackoverflow已经广泛涵盖了这一点,但遗憾的是我在实现我的特定代码时失去了。

基本上我有一个充满产品的列表页面,某些产品列出了两次,三次甚至更多次。这是一个很好的理由,但无论如何。

我想按产品名称进行分组,以便具有相同名称的各种产品在列表页面上不会出现多次。但是我希望价格最低的那个是由该组选择显示的那个。这是代码:

$listing_sql = "SELECT DISTINCT " . $select_column_list . "
p.products_id, p.products_type, p.master_categories_id,
p.manufacturers_id, p.products_price,  p.products_tax_class_id,  
pd.products_description, pd.products_name, IF(s.status = 1,
s.specials_new_products_price, NULL) as specials_new_products_price,  
IF(s.status =1, s.specials_new_products_price,
p.products_price) as final_price, p.products_sort_order,
p.product_is_call, p.product_is_always_free_shipping,
p.products_qty_box_status";

$listing_sql .= " FROM " . TABLE_PRODUCTS . " p" .

" LEFT JOIN " . TABLE_SPECIALS . " s on p.products_id = s.products_id" .
" LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd on p.products_id =
pd.products_id" .
" JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c on p.products_id =
p2c.products_id" .
($filter_attr == true ? " JOIN " . TABLE_PRODUCTS_ATTRIBUTES . " p2a on
p.products_id = p2a.products_id" .
" JOIN " . TABLE_PRODUCTS_OPTIONS . " po on p2a.options_id =
po.products_options_id" .
" JOIN " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov on
p2a.options_values_id = pov.products_options_values_id" .
(defined('TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK') ? " JOIN " .
TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . " p2as on p.products_id =
p2as.products_id " : "") : '');

$listing_sql .= " WHERE p.products_status = 1
and p.products_quantity > 0
and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
and p2c.categories_id = '" . (int)$current_category_id . "'" .
$filter .
" group by pd.products_name " .
$having .
$alpha_sort;

该小组在近端实施:

" group by pd.products_name " .

工作正常。但显然它是由它找到的第一个组成的,因此如果它不是输入数据库的产品的第一个版本,那么价格最低的那个就会被埋没。与查询类似,price列为:

p.products_price

我看过并试图从这里实现一些例子: Select the 3 most recent records where the values of one column are distinct

我无法让他们正确 - 我对MySQL查询的理解非常糟糕。任何帮助将不胜感激。

更新:

SELECT  DISTINCT  p.products_id, p.products_type, p.master_categories_id,
p.manufacturers_id, p.products_price,  p.products_tax_class_id, 
pd.products_description, pd.products_name, IF(s.status = 1,
s.specials_new_products_price, NULL) as specials_new_products_price,     
IF(s.status =1, s.specials_new_products_price, p.products_price) as 
final_price, p.products_sort_order, p.product_is_call, 
p.product_is_always_free_shipping, p.products_qty_box_status

FROM zen_products p
LEFT JOIN zen_specials s on p.products_id =   
s.products_id
LEFT JOIN zen_products_description pd on p.products_id = pd.products_id
JOIN zen_products_to_categories p2c on p.products_id = p2c.products_id
WHERE p.products_status = 1
and p.products_quantity > 0
and pd.language_id = '1'
and p2c.categories_id = '3'

GROUP BY pd.products_name

1 个答案:

答案 0 :(得分:0)

我没有涉及所有这些,但一般的解决方案如下:

SELECT x.* -- DISTINCT would be redundant in this context.
  FROM my_table x
  JOIN 
     ( SELECT grouping_column
            , MIN(ordering_column) n 
         FROM my_table 
        GROUP 
           BY grouping_column
     ) y
    ON y.grouping_column = x.grouping_column
   AND y.n = x.ordering_column;