A
A
Apxu2017-05-13 17:37:38
MySQL
Apxu, 2017-05-13 17:37:38

How to write a complex SLQ query?

da9db89c35f64d07b1b154a520cfac0f.jpg
There are 3 tables:
1. A table with cards
2. A table with decks
3. A table that links a card to a deck
For example, there are 4 elements in the card table:
id-1
id-2
id-3
id-4
in the deck table 2 elements:
id-1
id-2
and in table deck_card 6 element from
id-1, card_id-1, deck_id-1
id-2, card_id-2, deck_id-1
id-3, card_id-3, deck_id-1
id-4, card_id-2, deck_id-2
id-5, card_id-3, deck_id-2
id-6, card_id-4, deck_id-2
Such a request is needed, in which several card ids will be passed - for example, 2 and 3 (they are present in both decks) and it will return the deck id (deck.id).
In simple words - so that the intermediate table is checked and if the set with id-2 has elements with card_id - 2 AND card_id -3 , then deck_id is returned.
Sorry if I didn't write well. :|

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2017-05-13
@Apxu

SELECT `deck_id`
  FROM `deck_card`
  WHERE `card_id` IN (2, 3)
  GROUP BY `deck_id`
  HAVING COUNT(*) = 2

The (`deck_id`, `card_id`) pairs are assumed to be unique.

K
Konstantin Tsvetkov, 2017-05-13
@tsklab

Such a request is needed, in which
Nothing is sent to the request. There are requests with parameters. Decide.
SELECT DISTINCT id_desc FROM desc_card WHERE id_cart = 4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question