I found a weird way of how mysql does something. Someone please explain why it happens.
I select all the columns from table A, and left join from table B with a certain condition, that may or may not be met for each table A row.
Now after the joins, in the WHERE statement, I want to completely leave out rows (not the join part, but whole rows) that have a table B’s column categoryId of value 14 IF the there are table B results (if there aren’t, I do want the row to be kept).
So in WHERE I do WHERE tableB.categoryId != 14. There are no results (when there are supposed to be). For curiosity, I tried to rewrite the operator to =, so WHERE tableB.categoryId = 14, and the result was that the results I wanted I didn’t get, a and the results I didn’t want I did get.
I don’t understand this, this is weird.
Shouldn’t the not equals thing have worked?
I also tried !(tableB.categoryId = 14), but that also yielded no results. At the end, I tried WHERE IF(tableB.categoryId = 14, 0, 1), and that did work for some reason. Why did that work and why didn’t the not equals and negating operators work?
You need
WHERE tableB.categoryId IS NULL OR tableB.categoryId != 14