I have a PHP/MySQL app that has the following tables:
STORE: store_id(1), ... store_id(2), ... store_id(3), ... store_id(4), ... store_id(5), ... ZONE: zone_id(A), store_id(1) zone_id(A), store_id(2) zone_id(B), store_id(1) zone_id(B), store_id(3) zone_id(B), store_id(5) zone_id(C), store_id(4) zone_id(C), store_id(5) INVENTORY: inv_id, store_id, ... ...
As you can see, a store may be in more than one zone, but zone_id + store_id must be unique (ie, store may appear in a zone only once.)
Given only 'store_id', then:
How do I create a view of INVENTORY for all STOREs in same ZONE as the given 'store_id'?
Given a store_id
(identified below as ?
), this query gives you all other stores that belong to the same zone(s):
select store_id
from zone z
where exists (
select 1
from zone z1
where
z1.zone_id = z.zone_id
and z1.store_id = ?
)
You can turn this to a subquery and use in
to pull out the corresponding inventories:
select i.*
from inventory i
where i.store_id in (
select store_id
from zone z
where exists (
select 1
from zone z1
where
z1.zone_id = z.zone_id
and z1.store_id = ?
)
)
I assume that you mean in the same zone s as the store.
Write a query to select the zone of the store:
SELECT zone_id
FROM _
WHERE store_id = 'your_id'
We will use this as an inner query.
Write an outer query
CREATE VIEW _ AS
SELECT _
FROM _
WHERE store_id IN (Inner query) AND store_id <> 'your_id
to get all the stores in the same zone.
I'll let you build on that outer query to do any joins and selections you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.