SELECT SQL_CALC_FOUND_ROWS
a.id_product AS 'ID товара',
reference AS `Артикул` ,
b.name AS 'Название',
m.name AS 'Производитель',
a.price AS 'Цена',
sa.active AS 'Активный (0-Нет, 1- Да)',
a.available_for_order AS 'Доступен для заказа (0-Нет, 1- Да)',
concat('http://velodom.kiev.ua/',cl.`link_rewrite`,'/',a.`id_product`,'-',b.`link_rewrite`,'.html') as "ProductURL",
cl.name `Категория`, CONCAT('http://velodom.kiev.ua/img/p/',
-- now take all the digits separetly as MySQL doesn't support loops in SELECT statements
-- assuming we have smaller image id than 100'000 ;)
IF(CHAR_LENGTH(i.id_image) >= 5,
-- if we have 5 digits for the image id
CONCAT(
-- take the first digit
SUBSTRING(i.id_image, -5, 1),
-- add a slash
'/'),
''),
-- repeat for the next digits
IF(CHAR_LENGTH(i.id_image) >= 4, CONCAT(SUBSTRING(i.id_image, -4, 1), '/'), ''),
IF(CHAR_LENGTH(i.id_image) >= 3, CONCAT(SUBSTRING(i.id_image, -3, 1), '/'), ''),
if(CHAR_LENGTH(i.id_image) >= 2, CONCAT(SUBSTRING(i.id_image, -2, 1), '/'), ''),
IF(CHAR_LENGTH(i.id_image) >= 1, CONCAT(SUBSTRING(i.id_image, -1, 1), '/'), ''),
-- add the image id
i.id_image,
-- put the image extension
'.jpg') as image_url
FROM `vd_product` a
LEFT JOIN `vd_product_lang` b ON ( b.`id_product` = a.`id_product`
AND b.`id_lang` =1
AND b.`id_shop` =1 )
LEFT JOIN `vd_image` i ON ( i.`id_product` = a.`id_product` )
LEFT JOIN `vd_manufacturer` m ON ( a.`id_manufacturer` = m.`id_manufacturer` )
LEFT JOIN `vd_stock_available` sav ON ( sav.`id_product` = a.`id_product`
AND sav.`id_product_attribute` =0
AND sav.id_shop =1
AND sav.id_shop_group =0 )
JOIN `vd_product_shop` sa ON ( a.`id_product` = sa.`id_product`
AND sa.id_shop = a.id_shop_default )
LEFT JOIN `vd_category_lang` cl ON ( sa.`id_category_default` = cl.`id_category`
AND b.`id_lang` = cl.`id_lang`
AND cl.id_shop = a.id_shop_default )
WHERE 1
GROUP BY sa.id_product
ORDER BY a.`id_product` ASC