Search code examples
mysqlradixrule

Mysql - select with condition and input in array?


I have table like this

|  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   |
----------------------

I want to select with 2 condition in one input array, if I want to input (idsys = 1 and idsys = 2) so the output should be:

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

there anyone can help? thanks

-----QUESTION ADDITION----- this is the solution:

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

I want to add in "Where" condition "MAX(somefield)" it is available? so the code will be :

SELECT idrule
FROM tablename
WHERE MAX(somefiled) and
idsys IN (1, 2)
GROUP BY idrule
HAVING count(*) = 2

but not working? any Idea?


Solution

  • You could do this:

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