Adding column_name != 3 returns much fewer results
I have this database query:
select scheduled_hike_id
, hike_date
, hike_title
, hike_group_id
, hike_privacy
, hike_description
, DAYOFMONTH(hike_date)
, DAYNAME(hike_date)
, YEAR(hike_date)
, MONTH(hike_date)
from scheduled_hikes
where is_cancelled is null
and hike_date > DATE_ADD(NOW(), INTERVAL -1 DAY)
and show_on_home_page = 1
order by hike_date limit 4
It returns what I expected. When I add a clause "and hike_privacy != 3" it returns MUCH fewers resu开发者_JAVA技巧lts.
The other values in the column are NULL, 1, 2
Any ideas why the != 3 isn't working as I expected? Also, <3 is giving similarly unexpected results. I am using MySQL
try and (hike_privacy IS NULL OR hike_privacy != 3)
If hike_privacy is null
, it won't match hike_privacy != 3
.
Try: and (hike_privacy != 3 or hike_privacy is null)
Nulls never match non "null style" comparisons.
Note: Make sure you use brackets around your "OR" terms! In SQL, OR takes precedence, so without the brackets it would parsed as if it was coded like this (note the position of the brackets):
where ( is_cancelled is null
and hike_date > DATE_ADD(NOW(), INTERVAL -1 DAY)
and show_on_home_page = 1
and hike_privacy != 3
)
or hike_privacy is null
which is quite different from what we want, which is:
where is_cancelled is null
and hike_date > DATE_ADD(NOW(), INTERVAL -1 DAY)
and show_on_home_page = 1
and (
hike_privacy != 3
or hike_privacy is null
)
精彩评论