Let's say I own a bunch of malls that have stores and restaurants. I want to query for all malls in which at least 1 store OR at least 1 restaurants is open
There are three tables, malls, stores and restaurants.
malls
id | name |
---|---|
mall 1 | Josh's Mall |
mall 2 | Sarah's Mall |
stores
id | name | isOpen | mallId (foreign key) |
---|---|---|---|
1 | Express | false | mall 1 |
2 | Banana Republic | false | mall 2 |
restaurants
id | name | isOpen | mallId (foreign key) |
---|---|---|---|
1 | PF Changs | true | mall 1 |
2 | Panda Express | false | mall 2 |
Each individual store and restaurant is its own row within its respective table. Every store and restaurant contains a foreign key relation to the malls table that says that it belongs to that mall.
I want to query for all malls in which at least 1 store OR at least 1 restaurant is in stock. Since malls only have stores and restaurants, I'm basically querying malls that have something open
in this case the expected result would be Josh's mall since PF Changs is open
I've been working with this but it seems that you need to specify the foreign table for a .or() request.
.from("malls")
.select("name, store!inner( isOpen ), restaurants!inner( isOpen )")
.or("store.isOpen.eq.true, restaurants.isOpen.eq.true")
When I want to specify the foreign table separately, how do I include both?
.from("malls")
.select("name, stores!inner( isOpen ), restaurants!inner( isOpen )")
.or('isOpen.eq.true', { foreignTable: "stores" }) //but then where would i specify the restaurants table?
In psudo code, i want to do this
.from("malls")
.select("name, stores!inner( isOpen ), restaurants!inner( isOpen )")
.or(
.filter("stores.isOpen", "eq", true)
.filter("restaurants.isOpen", "eq", true)
)
Any help would be greatly appreciated!