0

I have 2 tables (in MYSQL):

cards:

  • id
  • created_at
  • updated_at

card_values:

  • card_id
  • field_id
  • value
  • created_at
  • updated_at

The table cards has many card_values. How can I select:

A card that "has" a card_value where field_id = 2 AND

the SAME card "has" an other card_value where field_id = 3

Image: Select card where this conditions are true

  • This works, but seens wrong: SELECT * FROM cards WHERE EXISTS ( SELECT * FROM card_values WHERE card_values.card_uuid = cards.uuid AND card_values.field_id = 2 AND JSON_EXTRACT(card_values.value, '$.value') in (111, '111') ) AND EXISTS ( SELECT * FROM card_values WHERE card_values.card_uuid = cards.uuid AND card_values.field_id = 21 AND JSON_EXTRACT(card_values.value, '$.value') in ('Cacaca') ) – Caio Lesnock Apr 16 '21 at 19:02
  • `SELECT * FROM cards c JOIN card_values cv ON cv.card_id = c.id WHERE cv.field_value = 2 OR cv.field_value = 3` .. should do the thing. – B001ᛦ Apr 16 '21 at 19:02

0 Answers0