我编写了一个查询来显示客户的客户 ID、客户姓名和联系方式,如果地址缺失,则显示电子邮件 ID,如果地址和电子邮件都丢失,则显示"NA",我正在根据客户 ID 对结果进行排序,但我没有得到任何输出。
我的表结构
customer_id VARCHAR(10)
customer_name VARCHAR(20)
address VARCHAR(20)
phoneno BIGINT
email_id VARCHAR(20)
select customer_id,customer_name,phone_no,email_id from customers ,
where
if(address isnull) and if(email_id isnull) then
"NA"
if(address isnull) then
email_id
order by customer_id;
触发查询
Program Outpput
customer_id customer_name phone_no email_id address
C001 Amith 9843123467 amith@gmail.com Bangalore
C002 Anand 9487256231 NULL NA
C003 Bala 9442478962 bala@gmail.com Chennai
C004 Gowri 9487267845 gowri@gmail.com Coimbatore
C005 Priya 9843134523 priya@gmail.com Cochin
C006 Prem 9528431343 prem@gmail.com Coimbatore
C007 Rahul 9513428433 rahul@gmail.com rahul@gmail.com
C008 John 9982843342 john@gmail.com Chennai
C009 Abdul 9843065418 abdul@gmail.com abdul@gmail.com
Expected Output
customer_id customer_name contact_details
C001 Amith Bangalore
C002 Anand NA
C003 Bala Chennai
C004 Gowri Coimbatore
C005 Priya Cochin
C006 Prem Coimbatore
C007 Rahul rahul@gmail.com
C008 John Chennai
C009 Abdul abdul@gmail.com
SELECT customer_id, customer_name, COALESCE(address, email_id, 'NA')
AS contact_details
FROM customers;
select customer_id,customer_name,
case
when email_id is null and address is null then 'NA'
when address is null then email_id
else address
end as 'CONTACT_DETAILS'
FROM customers
order by customer_id;
SELECT customer_id, customer_name, coalesce(address, email_id, 'NA') AS contact_details FROM customers ORDER BY customer_id ASC;
select customer_id,customer_name,
case
when(phone_no is null and address is null) then
email_id
when(phone_no is null) then
address
else
phone_no
end contact_details from customer order by customer_id asc;