select p.*,
(select z.plik from zdjecia z where z.id_album=p.id_album and tresc = 0 order by z.photo_order asc limit 1) as plik
from produkty p
where p.publikacja = '1' and p.id_kategoria in(select k.id_kategoria from kategorie k where k.lft between (select kk.lft from kategorie kk where kk.id_kategoria='18') and (select kk.rgt from kategorie kk where kk.id_kategoria='18') order by k.kategoria) and p.publikacja = '1' group by p.id_produkt order by p.nowosc desc, p.nazwa asc limit 0,12