-4

I have a table to store the dynamic attribute

The structure is like this:

id    itemid  value
----  ------  ------
1     1       A
2     2       B
3     3       C
4     1       B
5     2       C

So when I pass the multi-valued parameter

  • Pass value A B result itemid 1
  • Pass value B result itemid 1 2
  • Pass value B C result 2

How can I write this sql? By the way i use mysql

chanjianyi
  • 607
  • 4
  • 15
  • 35

2 Answers2

2

Assuming that (itemid, value) has a UNIQUE constraint:

SELECT itemid
FROM tableX
WHERE value IN ('A', 'B')       -- the list
GROUP BY itemid
HAVING COUNT(*) = 2 ;           -- the size of the list
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
1

You should be able to get the behaviour you are looking for using a SQL INTERSECT query. Here is a brief example.

SELECT itemid FROM mytable WHERE value = 'A' INTERSECT SELECT itemid FROM mytable WHERE value = 'B'

Christian Dahlqvist
  • 1,665
  • 12
  • 9