SELECT MAX(val) val FROM (SELECT MAX(LEAST(a.cena_ddv, a.cena_ddv_pop)) val FROM artikli a WHERE a.aktivnost='Y' AND a.kat_id IN (SELECT kat_id FROM kategorije WHERE permalink='sifon-za-kopalne-kadi') UNION SELECT MAX(LEAST(ap.cena, ap.cena_akcija)) val FROM artikli_podrobnosti ap, artikli a WHERE ap.aktivnost='YES' AND a.art_id = ap.artikel_id AND a.kat_id IN (SELECT kat_id FROM kategorije WHERE permalink='sifon-za-kopalne-kadi')) a
SELECT SQL_CALC_FOUND_ROWS at.art_id, at.sifra, at.nazivk, at.nazivd, at.cena, at.cena_pop, at.aktivnost, at.opisk, at.brand_id, at.znacka, at.ime_dat, at.alt, k.naziv, at.kat_id, at.zaloga, at.date_added datum, at.artPermalink, at.katPermalink, at.p_outlet, at.podrobnosti_id, at.vec_moznosti, at.zaloga_kosov, at.barva_id, at.sticker_text
FROM (
SELECT sart.art_id, sart.sifra, sart.nazivk, sart.nazivd, sart.cena, sart.cena_pop, sart.kat_id, sart.aktivnost, sart.opisk, sart.brand_id, sart.znacka, sart.ime_dat, sart.alt, sart.trgovina, sart.zaloga, sart.date_added, sart.artPermalink, sart.katPermalink, sart.p_outlet, sart.podrobnosti_id, sart.vec_moznosti, sart.zaloga_kosov, sart.barva_id, sart.sticker_text
FROM (
SELECT DISTINCT a.art_id, a.sifra, a.nazivk, a.nazivd, a.cena, a.cena_pop, a.kat_id, a.aktivnost, a.opisk, a.trgovina, a.brand_id, a.znacka, s.ime_dat, s.alt, a.zaloga, a.date_added, a.artPermalink, a.katPermalink, a.p_outlet, a.podrobnosti_id, a.vec_moznosti, a.zaloga_kosov, a.barva_id, a.sticker_text
FROM (SELECT DISTINCT artikli.art_id, artikli.sifra,
IFNULL((SELECT text FROM translations_data td WHERE td.group_name = 'artikli' AND td.field='nazivk' AND td.doc_id = artikli.art_id AND td.language_id = 593), artikli.nazivk) nazivk,
IFNULL((SELECT text FROM translations_data td WHERE td.group_name = 'artikli' AND td.field='nazivd' AND td.doc_id = artikli.art_id AND td.language_id = 593), artikli.nazivd) nazivd,
IF (vec_moznosti='Y', ap.cena, artikli.cena_ddv) cena, IF (vec_moznosti='Y', ap.cena_akcija, artikli.cena_ddv_pop) cena_pop,
artikli.kat_id, artikli.aktivnost, artikli.opisk, artikli.trgovina, artikli.brand_id,
IFNULL((SELECT text FROM translations_data td WHERE td.group_name = 'artikli' AND td.field='znacka' AND td.doc_id = artikli.art_id AND td.language_id = 593), artikli.znacka) znacka,
artikli.zaloga, artikli.date_added, artikli.permalink artPermalink, k_pool.katPermalink, artikli.p_outlet,
ap.id podrobnosti_id, artikli.vec_moznosti, artikli.zaloga_kosov, ap.barva_id, artikli.sticker_text
FROM artikli LEFT JOIN artikli_podrobnosti ap ON ap.id = (SELECT ap1.id FROM artikli_podrobnosti ap1, artikli_podatki aap WHERE ap1.artikel_id = artikli.art_id AND ap1.aktivnost='YES' AND aap.id = ap1.barva_id AND ap1.cena>0 ORDER BY ap1.cena DESC, ap1.id ASC LIMIT 1)
INNER JOIN (SELECT root.naziv as root_naziv, root.kat_id as root_id, root.permalink as katPermalink, parent1.kat_id as parent1_id, parent2.kat_id as parent2_id, parent3.kat_id as parent3_id
FROM kategorije as root
LEFT OUTER JOIN kategorije as parent1 ON (root.kat_id = parent1.kat_id_nadr AND parent1.aktivnost='Y')
LEFT OUTER JOIN kategorije as parent2 ON (parent1.kat_id = parent2.kat_id_nadr AND parent1.aktivnost='Y')
LEFT OUTER JOIN kategorije as parent3 ON (parent2.kat_id = parent3.kat_id_nadr AND parent1.aktivnost='Y')
WHERE root.aktivnost='Y' AND root.permalink = 'sifon-za-kopalne-kadi') k_pool on
(artikli.art_id IN (SELECT prd_id FROM products_categories pc0 WHERE cat_id = k_pool.root_id) OR
artikli.art_id IN (SELECT prd_id FROM products_categories pc0 WHERE cat_id = k_pool.parent1_id) OR
artikli.art_id IN (SELECT prd_id FROM products_categories pc0 WHERE cat_id = k_pool.parent2_id) OR
artikli.art_id IN (SELECT prd_id FROM products_categories pc0 WHERE cat_id = k_pool.parent3_id))
) a
LEFT JOIN (
SELECT DISTINCT ime_dat, alt, artikel
FROM slike
WHERE sort = 1
ORDER BY sort ASC
) s ON (a.art_id = s.artikel)
GROUP BY a.art_id
) sart
) at
LEFT JOIN kategorije k ON (at.kat_id = k.kat_id AND k.aktivnost='Y')
WHERE at.aktivnost = 'Y' AND at.p_outlet='N' AND at.sifra <> '-1'
ORDER BY IF(at.zaloga='Y',1,2) ASC, at.nazivk ASC, at.nazivd ASC LIMIT 0, 20;