开发者

how to select for multiple column values including NULL without repeating column name?

So far I'm doing it this way:

SELECT * FROM table_name WHERE column in(var1,var2,var3) or column is NULL

I have issues with repeating column name two times here since in() can't take null as an argument, it returns also no error, just 0 columns. (Maybe some magic variables that can refer last 开发者_如何学运维called column ?)


This:

'column'

is a string literal, not a column name. So this:

'column' in(var1,var2,var3) or 'column' is NULL

won't match anything unless var1, var2, or var3 happen to be the string 'column'. Try dropping the quotes on the column name:

SELECT * FROM table_name WHERE column in (var1, var2, var3) or column is NULL


Another option without repeating column name:

SELECT * FROM table_name WHERE coalesce(column,'NULL') in (var1,var2,var3,'NULL')


You could do a union select:

SELECT 
  * 
FROM 
  `table_name` 
WHERE 
  `column` IN (var1, var2, var3)
UNION
  SELECT
    *
  FROM
    `table_name`
  WHERE
    `column` IS NULL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜