0

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.

3
  • 2
    You probably should add some parenthesis around your clauses. Mixing OR and AND doesn't do what you think. Commented Nov 7, 2014 at 23:45
  • 2
    Use some parantheses: (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 = ?") Commented Nov 7, 2014 at 23:45
  • 1
    P.S. Replace deck_id = ? OR deck_id = ? OR deck_id = ? with deck_id IN (?,?,?). Commented Nov 7, 2014 at 23:46

2 Answers 2

3
"SELECT * FROM cards WHERE 
  (card_one LIKE ? OR card_two LIKE ? OR card_three LIKE ?) 
  AND deck_id in (id1,id2,id3) 
Sign up to request clarification or add additional context in comments.

Comments

2

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 = ? )"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.