I know this has been extensively covered on Stackoverflow, but unfortunately I'm lost on the implementation for my specific bit of code.
Basically I have a listing page full of products, and certain products are listed twice, thrice or even more times. For a very good reason, but anyways.
I want to group by product name, so that the various products that have the same name don't show up more than once on the listing page. But I want the one with the lowest price to be the one selected to be displayed by the group by. Here is the code:
$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;
The group by implemented near the end:
" group by pd.products_name " .
works fine. But obviously it groups by the first one it found, and so the one with the lowest price gets buried away if it was not the first version of the product entered into the database. The price column, as like in the query, is:
p.products_price
I've looked at and tried to implement some examples from here: Select the 3 most recent records where the values of one column are distinct
I just can't get them right - my understanding of MySQL queries is just really bad. Any help would be appreciated.
UPDATE:
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