V
V
Vivica2019-04-21 13:19:34
SQL
Vivica, 2019-04-21 13:19:34

What request to make selection from the table of a DB?

Given: item_tags table

ID   |  TAG_ID |  ITEM_ID
--------------------------
  1  |    4    |     5
--------------------------
  2  |    2    |     6
--------------------------
  3  |    1    |     4
--------------------------
  4  |    2    |     2
--------------------------
  5  |    3    |     2
--------------------------
  6  |    4    |     6

Question: how to get a common TAG_ID for them from a given list of ITEM_ID?
Examples:
1) For ITEM_ID = 2 and ITEM_ID = 6, the result will be a common TAG_ID = 2
2) For ITEM_ID = 5 and ITEM_ID = 6, the result will be a common TAG_ID = 4
At first it seemed that it was not difficult, but now I'm completely confused)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VicTHOR, 2019-04-21
@Vivica

Something like this

SELECT f.tag_id Результат
    FROM item_tags f
    JOIN item_tags s
    ON f.tag_id = s.tag_id 
        AND f.item_id = 2 
        AND s.item_id = 6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question