I have a problem with DISTINCT. I don't know if you can say to mysql: find me distinct this column and that but return info from other columns as well (from the first row according to order by). To elaborate more
First the tables:
products, - stores product information
products_lang, - language dependent fields
localization,- shops
products_localizations_xref, - connects products and shops
promotions, - only products that have certain promotion are displayed
products_categories_xref_search, - we can say what category the product is in
firms, - a firm can have many localizations
address - stores parts of the address (street, numer, flat etc.)
the query I have now:
if (!empty($name)) $name_sql = " AND (products_lang.name like '%$name%' OR products_lang.short_name like '%$name%' )";
else $name_sql = "";
$sql_prototype = 'select DISTINCT products.id as pid,localization.id as lid,localization.x as fx,
localization.y as fy,products.picture as img,products.price_before as pbefore,
products.price_after as pafter, promotions.date_stop as valid, products_lang.name as fullname,
products_lang.short_name as shortname, products_lang.description as pdescription,
firms.name as fname, address.code as fcode, address.city as fcity, address.street as fstreet,
address.number as fnumber, address.flat as fflat, localization.tel as ftel,
localization.fax as ffax, localization.www as fwww, localization.email as femail
, POW( localization.x - '.$x.', 2 ) + POW( localization.y -'.$y.', 2 ) AS distsq
from products,products_lang,localization,products_localizations_xref,promotions,products_categories_xref_search,firms,address
where products_localizations_xref.product_id = products.id
and products_localizations_xref.localization_id = localization.id
and localization.id = promotions.sub_id
and promotions.type = 4
AND promotions.date_start <= CURDATE( )
AND promotions.date_stop >= CURDATE( )
AND x > '.($x-$radius).'
AND x <'.($x+$radius).'
AND y > '.($y-$radius).'
AND y <'.($y+$radius).'
AND firms.auth_id = localization.auth_id
AND localization.address = address.id
AND products_lang.product_id = products.id
AND products_lang.lang = \''.$_SESSION['language'].'\'
'.$name_sql.'
AND products_categories_xref_search.product_id = products.id
AND products_categories_xref_search.category_id = \'_CAT_\'
ORDER BY distsq
';
What it does is basically select all products with a given name or category which belong to localization within the square of 2x radius centered at $x,$ y, and that localization has to have certain promotion to be searched.
If a product belongs to two localizations DISTINCT doesnt treat those rows as the same (because localization data is different). What I would like to achieve is to select distinct products.id and the rest info should go from the localization closest to $x, $y.
I tried to find something about distinct in mysql doc but it's not as userfriendly as php doc. Any ideas?