Domanda

I have been working on a product database schema and one of the requirements is that in order to consider a product as discontinued all of the product skus must be set to discontinued.

Product Table:
id
slug
name

One Product to Many ProductSkus

ProductSku Table
id
slug
name
product_id
product_sku_availability_id

One ProductSku to One ProductSkuAvailability

ProductSkuAvaliability
id
slug
name

now I don't really need an exact query, but is there a way to only select products where all productSkus.product_sku_availability are discontinued? here is a sample of what I am asking:

| productId | productSkuId | availabilityId |
| 1         | 1            | 1              |
| 1         | 2            | 1              |
| 2         | 3            | 2              |
| 2         | 4            | 1              |

in the above sample I would only want to select the productId = 1 because both product skus have an availabilityId of 1.

È stato utile?

Soluzione

If the value of the column availabilityId has got the same value for every productId then are the maximum and minimum value equal. Because of that should a

GROUP BY productId 

with

HAVING (MIN(availabilityId) = 1 AND MAX(availabilityId) = 1)

produce the desired result.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top