0

I am try to get all the rows in parts_keywords not included in parts_questions. Unfortunately, I am getting all the rows of both tables.

SELECT *FROM parts_keywords 
    LEFT OUTER JOIN parts_questions ON parts_keywords.ID = parts_questions.question_id
     WHERE parts_questions.question_id IS NULL
     AND parts_keywords.term  LIKE '%where%' || parts_keywords.term LIKE '%how%' || parts_keywords.term LIKE '%who%' || parts_keywords.term LIKE '%what%' 
    ORDER BY parts_keywords.ID DESC lIMIT 15

I have tried using the OR instead of ||. I have also tried using MATCH instead of LIKE to no avail.

When i use only one LIKE element, such as 'who' i get the correct results. However, it is important for me to get result for all LIKE elements together.

Please help

Aldwoni
  • 1,168
  • 10
  • 24
earnest
  • 261
  • 2
  • 4
  • 9

2 Answers2

0

you're missing () around your OR clauses

SELECT *
FROM parts_keywords 
  LEFT OUTER JOIN parts_questions ON parts_keywords.ID = parts_questions.question_id
WHERE parts_questions.question_id IS NULL
AND (parts_keywords.term  LIKE '%where%' 
 OR parts_keywords.term LIKE '%how%'
 OR parts_keywords.term LIKE '%who%'
 OR parts_keywords.term LIKE '%what%')
ORDER BY parts_keywords.ID DESC lIMIT 15
Matt Busche
  • 14,216
  • 5
  • 36
  • 61
0

Try using paranthesis

SELECT * FROM parts_keywords pk
LEFT OUTER JOIN parts_questions pq ON pk.ID = pq.question_id
WHERE pq.question_id IS NULL
AND ( pk.term LIKE '%where%' OR pk.term LIKE '%how%' OR pk.term LIKE '%who%' OR pk.term     LIKE '%what%' )
ORDER BY parts_keywords.ID DESC lIMIT 15
ilikemypizza
  • 362
  • 2
  • 13