0

我有以下查询:

SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user

我将如何做相当于:

SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user 
    **WHERE full_name IS NOT NULL**

(产生一个Unknown column 'full_name' in 'where clause'

4

2 回答 2

2

解决这个问题的两种方法:

SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user 
WHERE first_name IS NOT NULL and last_name is not null

或者,

select * from (
    SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user)
where full_name is not null
于 2012-05-30T06:06:32.403 回答
0
SELECT concat(IF(first_name IS NOT NULL, first_name, ''), ' ', IF(last_name IS NOT NULL, last_name, '')) as full_name FROM auth_user;

在 where 子句中使用 IS NOT NULL 和在 IF 子句中使用 IS NOT NULL 的区别在于 where 子句将消除(过滤)具有空值的行,而 if 子句将获取所有行但具有 full_name不是“未知”,它只会丢失空组件

于 2012-05-30T06:06:53.087 回答