MySQL SELECT where IN (associative array[key]) AND SUM(Col5) IN >= (array[key2])

MySQL SELECT where IN (associative array[key]) AND SUM(Col5) IN >= (array[key2])

假设我有一个 table 颜色列 MainColor (Green, blue), Tone(44,17), Brand(Mybrand, Yourbrand, etc), Type(Standard, Special) and Availability 我需要的是根据这样的请求显示:

Looking for 3x Green Tone 44, 8x Green Tone 17 and 5x Blue Tone 44

存储在这样一个数组中:

Array
(
    [0] => Array
        (
            [MainColor] => Green
            [Tone] => 44
            [Needed] => 3
        )
    [1] => Array
        (
            [MainColor] => Blue
            [Tone] => 44
            [Needed] => 5
        )
    [2] => Array
        (
            [MainColor] => Green
            [Tone] => 17
            [Needed] => 8
        )

)

类似

There are 3x Green Tone 44 (1x MyBrand Normal 1x Mybrand Special and 1x Yourbrand Standard)

and 8x Green Tone 17 (1x Herbrand Special 2x Herbrand Standard, 4x Ourbrand Standard and 1x Hisbrand Special)

and 5x Blue Tone 44 (3Mybrand Special and 2x Yourbrand Special)

available color(s)

我知道我可以用类似的东西实现前两个陈述:

SELECT *
    FROM Colors  
    WHERE MainColor IN (array['MainColor']) AND Tone IN (array['Tone'])

那么第三个需要求和比较的(大于等于需求量)呢?

SELECT *
    FROM Colors  
    WHERE MainColor IN (array['MainColor']) AND Tone IN (array['Tone']) AND SUM(Colors.Availability) IN >= (array['Needed'])

我认为您需要找到所有具有该特定 colour/tone 的品牌,因此忽略数量,如果有可用的就列出它们。

然后让用户 select 在网页中输入他们想要的每个品牌的数量。在这一点上,您可以强制他们 select 的品牌总数与他们已经输入的一样(可以在提交之前做一些 JS 来做到这一点),或者可以简单地让他们有多少他们想要的。

唯一的困难是,如果向两个人提供相同的 colours/tones 并且他们都尝试订购比您拥有的更多的库存。然后,您将不得不检查他们何时最终订购它们是否仍然存在库存。