Mysql - select 条件和数组输入?
Mysql - select with condition and input in array?
我有table这样的
| idrule | idsys |
----------------------
|R01#1 | 1 |
|R01#1 | 2 |
|R01#2 | 1 |
|R01#2 | 3 |
|R01#2 | 4 |
|R01#3 | 2 |
|R01#3 | 1 |
|R01#3 | 5 |
|R01#4 | 1 |
|R01#4 | 4 |
|R01#4 | 3 |
----------------------
我想 select 在一个输入数组中使用 2 个条件,如果我想输入(idsys = 1 和 idsys = 2),那么输出应该是:
| idrule |
----------
|R01#1 |
|R01#3 |
----------
有没有人可以帮忙?谢谢
-----问题补充-----
这是解决方案:
SELECT idrule
FROM tablename
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
我想在"Where"条件下添加"MAX(somefield)"可以吗?所以代码将是:
SELECT idrule
FROM tablename
WHERE MAX(somefiled) and
idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
但不工作?任何的想法?
SELECT idrule
FROM *table* t
JOIN *table* ct ON t.idrule = ct.idrule
WHERE t.idsys=1 AND ct.idsys=2;
你可以这样做:
SELECT idrule
FROM tablename
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
我有table这样的
| idrule | idsys |
----------------------
|R01#1 | 1 |
|R01#1 | 2 |
|R01#2 | 1 |
|R01#2 | 3 |
|R01#2 | 4 |
|R01#3 | 2 |
|R01#3 | 1 |
|R01#3 | 5 |
|R01#4 | 1 |
|R01#4 | 4 |
|R01#4 | 3 |
----------------------
我想 select 在一个输入数组中使用 2 个条件,如果我想输入(idsys = 1 和 idsys = 2),那么输出应该是:
| idrule |
----------
|R01#1 |
|R01#3 |
----------
有没有人可以帮忙?谢谢
-----问题补充----- 这是解决方案:
SELECT idrule
FROM tablename
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
我想在"Where"条件下添加"MAX(somefield)"可以吗?所以代码将是:
SELECT idrule
FROM tablename
WHERE MAX(somefiled) and
idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2
但不工作?任何的想法?
SELECT idrule
FROM *table* t
JOIN *table* ct ON t.idrule = ct.idrule
WHERE t.idsys=1 AND ct.idsys=2;
你可以这样做:
SELECT idrule
FROM tablename
WHERE idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2