Im trying to filter an query, as some results are not needed I dont want them in the output, in my example im trying to select only the results from the table where to column ‘route’ = a blog, offer or faq, this works only if I use 1 condition, when I am more it does not return any data.
$slugs = $query->select( 'a.language , a.translation , a.cms_module_id' )
->from( ['a' => 'cms_module_translation'] )
->leftJoin( ['b' => 'cms_module'] ,
// below the filter conditions
'b.id = a.cms_module_id AND b.route = "blog" AND b.route = "faq" AND b.route = "offer" ' )
->where( ['b.is_deleted' => 0,
'b.is_enabled' => 1,
'a.attribute' => 'slug'
])
->all();
mdomba
(Maurizio Domba Cerin)
January 31, 2018, 9:08am
2
As you wrote above you want ‘route’ to be blog OR offer OR faq… but in the query you put blog AND offer AND faq… so you get empty result
When replacing the AND for OR it will output every line double and it will output everything so thats not working for some reason.
mdomba
(Maurizio Domba Cerin)
January 31, 2018, 9:37am
4
That’s a step further as before you where getting none
Did you pay attention to group those conditions like
b.id = a.cms_module_id AND (b.route = "blog" OR b.route = "faq" OR b.route = "offer")
That’s a step further as before you where getting none
Did you pay attention to group those conditions like
b.id = a.cms_module_id AND (b.route = "blog" OR b.route = "faq" OR b.route = "offer")
Okay you have to group them…nice its now working. Thanks
mdomba
(Maurizio Domba Cerin)
January 31, 2018, 9:55am
6
It all depends on your needs… in your case if you don’t group them the expresion would be the same as
(b.id = a.cms_module_id AND b.route = "blog") OR b.route = "faq" OR b.route = "offer"
this way you get all "faq" and all "offer" but only "blogs" where id=cms_module_id, and that is not what you want so you need to group them properly