Mysql - 选择where where子句和附加条件?

时间:2015-08-22 11:39:03

标签: conditional-statements

这是表格:

* rulestbl

|  idrule  |  idsys  |  orders |
--------------------------------
|R01#1     |    1    |    1    |
|R01#1     |    2    |    1    |
|R01#2     |    1    |    2    |
|R01#2     |    3    |    2    |
|R01#2     |    4    |    2    |
|R01#3     |    2    |    3    |
|R01#3     |    1    |    3    |
|R01#3     |    5    |    3    |
|R01#4     |    1    |    4    |
|R01#4     |    4    |    4    |
|R01#4     |    5    |    4    |
--------------------------------

我想在一个输入数组中选择2个条件,如果我想输入(idsys = 1idsys = 2)和"where orders" = MIN(),那么输出应为:

|  idrule  |  orders  |
----------------------
|R01#1     |   1     |
----------------------
谁能帮帮我?感谢

结果应该只消除“idsys with conditon 1 and 2”,所以结果应该是:

|   idrule  |  orders  |
-----------------------
|R01#1      |    1    |
|R01#3      |    3    |
-----------------------

这是首次消除的语法:

SELECT idrule
FROM rulestbl
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2

所以接下来我们消除MIN(顺序)是1所以最终的结果是:

|   idrule  |  orders  |
------------------------
|R01#1      |    1     |
------------------------

1 个答案:

答案 0 :(得分:0)

这应该有用; - )

SELECT X.IDRULE, MIN(X.ORDERS) FROM rulestbl,
(
SELECT idrule, orders
FROM rulestbl
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
) X
WHERE X.idrule = rulestbl.idrule

在IN子句中,您可以拆分数组。

祝你好运