pixie icon indicating copy to clipboard operation
pixie copied to clipboard

Multiple conditional joins

Open scott-wmm opened this issue 8 years ago • 2 comments

Hey,

I need the ability to do multiple joins with a conditional 'AND' on each one.

For example the current query builder reads:

            $products
                ->join('product_to_categories', 'products.id', '=', 'product_to_categories.product')
                ->where(function($query) use($categories) {
                    foreach($categories as $category) {
                        $query->orWhere('product_to_categories.category', $category);
                    }
                });

However this returns all results that match all the given categories. I need to be able to only return the results that match both. Changing the 'orWhere' to simply 'where' returns no results as the singular JOIN can't match multiple conditions.

The raw query I'm looking for is roughly:

JOIN product_to_categories AS ptoc1
ON ptoc1.product =products.id AND (ptoc1.category_id='4')
JOIN product_to_categories AS ptoc2
ON ptoc2.product =products.id AND (ptoc2.category_id='5')

I've spent a few hours experimenting, can this be achieved using Pixie?

Thank you.

scott-wmm avatar Sep 14 '17 12:09 scott-wmm

@TCB13 can you please check the issue?

usmanhalalit avatar Nov 24 '17 10:11 usmanhalalit

@scott-wmm I might be getting the wrong ideia on this, can you provide us with a dump of a a test database / data where I can do some tests?

TCB13 avatar Dec 13 '17 14:12 TCB13