v2.5.2
Giriş yap

ÜRÜN FİLTRELERİNİ UYGULAMA

isa
371 defa görüntülendi

Ürünlere ait özellikleri filtrelemek istiyorum.

Veritabanı yapım şu şekilde;

filters

idtypenametitle
11Erkek Pantolon RenkRenk
22Erkek Pantolon BedenBeden

filter_option

idfilter_idvalue
11Kırmmızı
21Siyah
3230
4231

category_filters

idcategory_idfilter_id
121
222

product_filters

idfilter_idfilter_option_idproduct_id
1111
2121
3211

Örnek olarak veri tabanı yapım bu şekilde.

select products.* from `product_filters` 
inner join `products` on `products`.`id` = `product_filters`.`product_id` 
where 
`product_filters`.`filter_option_id` in ('4') 
OR 
`product_filters`.`filter_option_id` in ('10') OR
`product_filters`.`filter_option_id` in ('20') OR
(`products`.`category_id` = 2 or `products`.`parent_category_id` = 2)
WHERE 
(products.category_id = 2 or products.parent_category_id = 1)

Örnek olarak şu şekilde bir sql sorgusu yazdığım zaman bana veriler geliyor fakat or kullandığım için ürünler tekrara düşüyor.
Or yerine and kullanırsam hem filter_option_id si 4 , 10 ve 20 olan her hangi bir veri olmadığı için ürün bulunamıyor.
Bir şekilde product_id ve filter_id ye göre filter_option columnlarını tek colum haline getimem gerekli.
Bunu nasıl yapabilirim? Veya başka nasıl bir çözüm bulabilirim ?

@tayfunerbilen

Cevap yaz
Cevaplar (1)
isa
815 gün önce

product_filters tablosunda filter_option_id columunu doğruda ilişkilendirmek yerine. filter_options olarak tutum 'x$valx' şekilde filter optionları kaydettim ve tek satıra düşürüdüm.
like sorgusu ile de ürünlere ulaşmayı başardım. Böyle yapmak istemiyordum ama cevap gelmeyince mecbur böyle yaptık.Kullanmak isteyen var örne sql sorgusu.


select count(*) as aggregate from `product_filters` 
inner join `products` on `products`.`id` = `product_filters`.`product_id` 
where (`filter_options` LIKE '%x34x%' and `filter_options` LIKE '%x32x%' and 
`filter_options` LIKE '%x33x%' and `filter_options` LIKE '%x31x%') and (`filter_options` LIKE '%x26x%' 
and `filter_options` LIKE '%x27x%' and `filter_options` LIKE '%x28x%') 
and (`products`.`category_id` = 1 or `products`.`parent_category_id` = 1)