简体   繁体   English

SQL中NULL的情况

[英]Case for NULL in SQL

I have this code but its not working . 我有此代码,但无法正常工作。

When NULL is there it should give '' out and no title but it does not. 当有NULL时,它应该给出''out,并且没有标题,但是没有。

SELECT employee.pin,
CASE gender
WHEN 'M' or (employee.titel IS NULL) THEN CONCAT('Mister ', ' ', ' ', person.fname, ', ' )
WHEN 'W' or (employee.titel IS NULL) THEN CONCAT('Mrs ', ' ', ' ',     person.fname, ', ' )
WHEN 'M' THEN CONCAT('Mister ', employee.titel, ' ', person.fname, ', ' )
WHEN 'W' THEN CONCAT('Mrs ', employee.titel, ' ', person.fname, ', ' )
END AS salutation
FROM person,employee

where person.pin=employee.pin
ORDER BY pin

You can use CONCAT_WS that will just skip null values: 您可以使用CONCAT_WS来跳过空值:

SELECT
  employee.pin,
  CASE WHEN gender='M' THEN CONCAT_WS(' ', 'Mister', employee.titel, person.fname)
       WHEN gender='W' THEN CONCAT_WS(' ', 'Mrs', employee.titel, person.fname)
  END AS salutation
FROM
  person INNER JOIN employee
  ON person.pin=employee.pin
ORDER BY pin

You would have to use the other CASE syntax, because NULL is not equal to anything: 您将不得不使用其他CASE语法,因为NULL不等于任何值:

CASE
WHEN gender='M' or (employee.titel IS NULL) THEN CONCAT('Mister ', ' ', ' ', person.fname, ', ' )
WHEN gender='W' or (employee.titel IS NULL) THEN CONCAT('Mrs ', ' ', ' ',     person.fname, ', ' )
WHEN gender='M' THEN CONCAT('Mister ', employee.titel, ' ', person.fname, ', ' )
WHEN gender='W' THEN CONCAT('Mrs ', employee.titel, ' ', person.fname, ', ' )
WHEN gender IS NULL THEN ...
END AS salutation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM