如果在client_name后面有方括号,则尝试显示client_nickname,但是,如果client_nickname为NULL,则仍显示client_name。无法显示没有昵称的client_name。
尝试了WHERE语句,但仍仅显示带有昵称的名称。
this is a dummy database i created randomly
CREATE TABLE client (
client_id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
client_nickname VARCHAR (30),
client_mobile int(10),
client_home int(10),
client_office int(10)
);
INSERT INTO client (client_id, firstname, lastname, client_nickname, client_mobile, client_home, client_office)
VALUES ('1', 'Doe', 'John', 'Johnny', '11223344',NULL, '66778899');
INSERT INTO client (client_id, firstname, lastname, client_nickname, client_mobile, client_home, client_office)
VALUES ('2', 'Mary', 'Lee', NULL, '99887766','44556677', '66778899');
INSERT INTO client (client_id, firstname, lastname, client_nickname, client_mobile, client_home, client_office)
VALUES ('3', 'Dreylan', 'Zhou', 'LilMonster', NULL, NULL, '66770000');
The code I worked out so far to achieve what i want
SELECT CONCAT (firstname, " ", lastname, ' (', client_nickname, ')') AS client_name
FROM client;
我希望列“ client_name”中包含所有3个这样的客户端名称:
Doe John (Johnny)
Mary Lee
Dreylan Zhou (LilMonster)
但实际输出仅为:
Doe John (Johnny)
Dreylan Zhou (LilMonster)
答案 0 :(得分:0)
您是否需要检查null
SELECT case when client_nickname is null
then CONCAT(firstname, " ", lastname)
else CONCAT(firstname, " ", lastname, ' (', client_nickname, ')')
end AS client_name
FROM client;