2017-10-04 34 views
1

我有一块SQL查询返回的结果与email_address列名在上段设置是这样的:如何从结果集输出中隐藏列标题?

| email_adress | 
|-------------------| 
| [email protected] | 
| [email protected] | 

我需要什么,而不是无的第一行设置类似的结果:

| [email protected] | 
| [email protected] | 

这里是我的查询:

SELECT email_address 
FROM company_digital 
WHERE email_address IS NOT NULL 
AND email_address IS NOT NULL 
AND hash_id >= 700 
AND hash_id < 800 
UNION 
SELECT email_address_2 
FROM company_digital 
WHERE email_address_2 IS NOT NULL 
AND email_address_2 IS NOT NULL 
AND hash_id >= 700 
AND hash_id < 800 
UNION 
SELECT email_address_3 
FROM company_digital 
WHERE email_address_3 IS NOT NULL 
AND email_address_3 IS NOT NULL 
AND hash_id >= 700 
AND hash_id < 800; 
+0

你想跳过第一个结果行或只显示没有列标题的结果? – tommyO

+0

是的,列标题 –

+2

这不是一个SQL语言问题。这将取决于具体使用哪个SQL客户端来运行查询。哪一个?您应该考虑在您的问题中添加该标签。 – tommyO

回答

1

假设你的意思列标题,然后给第一个字段在查询的别名,如:

select email_address AS Whatever 
from company_digital 
where email_address is not null 
and email_address is not null 
and hash_id >=700 and hash_id <800 
union 
select email_address_2 
from company_digital 
where email_address_2 is not null 
and email_address_2 is not null 
and hash_id >=700 and hash_id <800 
union 
select email_address_3 
from company_digital 
where email_address_3 is not null 
and email_address_3 is not null 
and hash_id >=700 and hash_id <800; 

这将输出:

| Whatever | 
_____________ 
[email protected] 

[email protected] 
_________________ 

或者

select email_address AS ' ' 
from company_digital 
where email_address is not null 
and email_address is not null 
and hash_id >=700 and hash_id <800 
union 
select email_address_2 
from company_digital 
where email_address_2 is not null 
and email_address_2 is not null 
and hash_id >=700 and hash_id <800 
union 
select email_address_3 
from company_digital 
where email_address_3 is not null 
and email_address_3 is not null 
and hash_id >=700 and hash_id <800; 

这将输出:

| | 
_____________ 
[email protected] 

[email protected] 
_________________ 

编辑清除代码:

SELECT email_address AS ' ' 
FROM company_digital 
WHERE email_address IS NOT NULL 
AND hash_id BETWEEN 700 AND 800 
UNION 
SELECT email_address_2 
FROM company_digital 
WHERE email_address_2 IS NOT NULL 
AND hash_id BETWEEN 700 AND 800 
UNION 
SELECT email_address_3 
FROM company_digital 
WHERE email_address_3 IS NOT NULL 
AND hash_id BETWEEN 700 AND 800; 

,或者根据您的数据......

SELECT CASE WHEN email_address IS NOT NULL 
      THEN email_address 
      WHEN email_address_2 IS NOT NULL 
      THEN email_address_2 
      WHEN email_address_3 IS NOT NULL 
      THEN email_address_3 
     END AS ' ' 
FROM company_digital 
WHERE hash_id BETWEEN 700 AND 800 
+1

标题垂直条和破折号仍然存在,所以我没有看到你的回答如何抑制列标题。 – RonJohn