1

I a have a MySQL db with 4 fields:

id | planchanged | dataremoved | rolloverenabled |
1  | Yes         | Yes         | Yes             |
2  | NULL        |             |                 |
3  |             | Yes         |                 |
4  | Yes         |             | Yes             |
5  |             |             | NULL            |

How do I query this db to show only records that doesn't have all 'Yes' on those three fields. Please also take into consideration that some of thos fields might be Null. So based on that example my result should show records 2,3,4 and 5.

Renee Cribe
  • 325
  • 1
  • 4
  • 14
  • 5
    "Please also take into consideration that some of thos fields might be Null" --- looks like a job description. Have you tried to solve your task yourself? – zerkms Jan 14 '13 at 02:16
  • yes I did but I can only get the result to show when all three are blank and any Null records are not coming up. Not sure what you mean by job description though. – Renee Cribe Jan 14 '13 at 02:22
  • 1
    And just for fun - the most straightforward condition that doesn't need to take `NULL` behaviour into account is: `NOT (planchanged = 'Yes' AND dataremoved = 'Yes' AND rolloverenabled = 'Yes')` – zerkms Jan 14 '13 at 02:31

3 Answers3

3

You can do this

SELECT * FROM TableName 
WHERE coalesce(planchanged,'-') <> 'Yes' OR 
coalesce(dataremoved,'-') <> 'Yes' OR
coalesce(rolloverenabled,'-') <>'Yes'

SQL FIDDLE DEMO

rs.
  • 26,707
  • 12
  • 68
  • 90
3
SELECT * 
FROM yourTableName 
WHERE planchanged IS NULL OR planchanged <> 'Yes' 
OR dataremoved IS NULL OR dataremoved <> 'Yes' 
OR rolloverenabled IS NULL OR rolloverenabled <> 'Yes'

SQL Fiddle

lc.
  • 113,939
  • 20
  • 158
  • 187
  • Aren't these redundant: planchanged IS NULL OR planchanged <> 'Yes' – Boundless Jan 14 '13 at 02:28
  • @Boundless: `NULL` never matches to anything. So `NULL = 'Yes'` == false, and `NULL <> 'Yes'` == false – zerkms Jan 14 '13 at 02:29
  • @Boundless Unfortunately, not for mysql (and most others depending on NULL matching settings). See http://www.sqlfiddle.com/#!2/2fdf4/3 – lc. Jan 14 '13 at 02:29
  • Thank you for the reply and all the comments I certainly learned some new stuff about NULL – Renee Cribe Jan 14 '13 at 02:40
0
SELECT * 
FROM  yourTableName 
WHERE planchanged IS NULL 
  OR  dataremoved IS NULL 
  OR  rolloverenabled IS NULL 
  OR  planchanged <> 'Yes' 
  OR  dataremoved <> 'Yes' 
  OR  rolloverenabled <> 'Yes'
Leigh
  • 28,765
  • 10
  • 55
  • 103
Boundless
  • 2,444
  • 2
  • 25
  • 40
  • "..show only records that doesn't have all 'Yes'..." shouldn't those OR's be AND's? – Class Jan 14 '13 at 02:21
  • no, they shouldn't. The query is correct. Please read the question. – Boundless Jan 14 '13 at 02:22
  • Great, I added is null check to the query. I didn't know that 'yes' <> NULL was false – Boundless Jan 14 '13 at 02:33
  • Yep, because `null` is not equal to anything. see http://stackoverflow.com/questions/1833949/why-is-null-not-equal-to-null-false – Leigh Jan 14 '13 at 02:42
  • It's funny that it's not equal to anything, yet it's not not equal either. – Boundless Jan 14 '13 at 02:46
  • Yeah, I remember thinking it was strange at first. But when you consider that `null` basically means "we do not know what the value is", there is no way to know what it does (or does *not*) equal. – Leigh Jan 15 '13 at 05:47