Home > Back-end >  SQL select from table where multiple entries exist
SQL select from table where multiple entries exist

Time:11-24

I have a database of info from the pokemon games. One of the tables contains the moves that can be learned by each pokemon. How can I select from this table where a pokemon can learn both of two moves?

My current query is SELECT * FROM 'learned-moves' WHERE 'Version Group'=? AND ('Move'=? OR 'Move'=?); but this selects all rows that contain either move. How can I only return those rows if they both match?

edit: db schema

CodePudding user response:

Consider an INNER JOIN on itself or self-join:

SELECT l1.Pokemon, l1.'Move' AS 'Move1', l2.'Move' AS 'Move2'
FROM 'learned-moves' l1
INNER JOIN 'learned-moves' l2
  ON l2.Pokemon = l1.Pokemon
  AND l2.'Version Group' = l1.'Version Group'
  AND l1.'Version Group' = ?
  AND l1.'Move' = ?
  AND l2.'Move' = ?
  •  Tags:  
  • sql
  • Related