你可以試試這樣一個查詢:
select product_option_id, product_option_value_id, min(quantity)
from product_option_value
where (product_option_id = 230 and product_option_value_id = 43)
or (product_option_id = 228 and product_option_value_id = 49)
group by product_option_id, product_option_value_id;
在實踐中:
> create table product_option_value(product_option_id int, product_option_value_id int, quantity int);
> insert into product_option_value values
> (230, 43, 2),
> (230, 32, 1),
> (228, 49, 1),
> (228, 50, 0),
> (229, 50, 1),
> (229, 49, 1),
> (230, 43, 8),
> (230, 32, 9),
> (228, 49, 11),
> (228, 50, 10),
> (229, 50, 5),
> (229, 49, 4);
> select product_option_id, product_option_value_id, min(quantity)
> from product_option_value
> where (product_option_id = 230 and product_option_value_id = 43)
> or (product_option_id = 228 and product_option_value_id = 49)
> group by product_option_id, product_option_value_id;
+-------------------+-------------------------+---------------+
| product_option_id | product_option_value_id | min(quantity) |
+-------------------+-------------------------+---------------+
| 228 | 49 | 1 |
| 230 | 43 | 2 |
+-------------------+-------------------------+---------------+
你也應該適當地引用查詢參數。使用像...product_option_id=".$key."...
這樣的結構(摘自你評論中的例子)很容易出錯,因爲$key
可能包含任何東西,如Little Boby Tables。
編輯:爲了得到所有這些羣體的最低限度,你剛落,由小組和分組列,就像這樣:
> select min(quantity)
> from product_option_value
> where (product_option_id = 230 and product_option_value_id = 43)
> or (product_option_id = 228 and product_option_value_id = 49);
+---------------+
| min(quantity) |
+---------------+
| 1 |
+---------------+
[標準化您的數據(http://stackoverflow.com/a/32620163) – Drew
以下代碼基於我的個人情況:'SELECT quantity FROM「。DB_PREFIX。」product_option_value WHERE product_option_id =「。$ key」。 AND product_option_value_id =「。$ value」。 AND subtract = 1「'我在PHP for循環中對所有的值對運行這個函數,我存儲了量化值,並且通過PHP獲得了最少的一個值 – user3980196
將來,使用編輯鏈接來包含關於你的問題的額外信息。沒有人可以在評論中閱讀代碼 – RiggsFolly