Trevor
Trevor

Reputation: 2457

Using LIKE and equal in SQL statement

Is there something wrong with this SQL statement? Specifically, in the "LIKE ? AND deck_id = ?" part.

"SELECT * FROM cards WHERE 
  card_one LIKE ? OR card_two LIKE ? OR card_three LIKE ? 
  AND deck_id = ? OR deck_id = ? OR deck_id = ? OR deck_id = ?
  OR deck_id = ? OR deck_id = ? OR deck_id = ?"

It searches for card_one and card_two properly, but not the third. If I take card_three out, then card_two does not work. Which leads me to believe that something in that area isn't right.

Can you use LIKE with AND in this way?

Desired results are

"SELECT * FROM cards WHERE (card1-3 LIKE ?) AND (Deck_id = decks_array)"

if that makes sense.

Upvotes: 0

Views: 57

Answers (2)

Andrej
Andrej

Reputation: 7504

"SELECT * FROM cards WHERE 
  (card_one LIKE ? OR card_two LIKE ? OR card_three LIKE ?) 
  AND deck_id in (id1,id2,id3) 

Upvotes: 3

Jacob Lambert
Jacob Lambert

Reputation: 7679

You need to add (...) around your conditions like so:

"SELECT * FROM cards WHERE 
 ( card_one LIKE ? OR card_two LIKE ? OR card_three LIKE ? )
 AND ( deck_id = ? OR deck_id = ? OR deck_id = ? OR deck_id = ?
       OR deck_id = ? OR deck_id = ? OR deck_id = ? )"

Upvotes: 2

Related Questions