8

可能重复:
在查询结果中包含一个空白行

我想在我的 SQL 查询最顶部的结果集中添加一个空行。

我正在使用的查询是这样的:

SELECT 
    PROFILETITLE, DOCID 
FROM
    PROFILES
WHERE 
    COMPANYCODE = ? 
ORDER BY 
    PROFILETITLE
4

5 回答 5

10

尝试使用union all

SELECT null as PROFILETITLE, null as DOCID 
UNION ALL
SELECT PROFILETITLE, DOCID 
FROM PROFILES
WHERE COMPANYCODE=? 
ORDER BY PROFILETITLE

但是如果你不想添加标题,如果DOCIDint类型,你必须使用union allcast如下

SELECT 'PROFILETITLE' as PROFILETITLE, 'DOCID' as DOCID 
UNION ALL
SELECT PROFILETITLE, CAST ( DOCID AS varchar(30) )
FROM PROFILES
WHERE COMPANYCODE=? 
ORDER BY PROFILETITLE
于 2012-10-10T08:36:29.703 回答
4

尝试这个:

SELECT '' AS PROFILETITLE, 0 AS DOCID
UNION ALL
SELECT PROFILETITLE, DOCID
FROM PROFILES WHERE COMPANYCODE = ? 
ORDER BY PROFILETITLE

请注意,在使用UNION和所有其他集合操作时,列必须在类型上匹配,因此您必须使用PROFILETITLE和为前两个空值起别名DOCID。而且您还必须注意两列的类型以进行匹配才能正常工作。可能您需要选择NULL而不是''and 0

于 2012-10-10T08:36:04.033 回答
3

varchar如果所有列都是空的,您可以添加一个“空行” ''

SELECT PROFILETITLE, DOCID 
FROM PROFILES
WHERE ...

UNION ALL

SELECT '' AS PROFILETITLE, '' AS DOCID

ORDER BY 
  CASE WHEN PROFILETITLE='' AND DOCID='' THEN 0 ELSE 1 END ASC
, PROFILETITLE ASC
于 2012-10-10T08:39:24.333 回答
0

您可以使用 SQLUnion

SELECT PROFILETITLE, DOCID 
FROM PROFILES
WHERE COMPANYCODE=? ORDER BY PROFILETITLE
UNION ALL
SELECT 0, '' 
FROM SYSIBM.SYSDUMMY1

您可以用您的临时表替换 SYSIBM.SYSDUMMY1

于 2012-10-10T08:36:43.393 回答
0

一个工会都可以完成这项工作

SELECT 
'' as PROFILETITLE,
'' as DOCID
UNION ALL
SELECT PROFILETITLE, DOCID 
FROM PROFILES
WHERE COMPANYCODE=? ORDER BY PROFILETITLE
于 2012-10-10T08:39:15.330 回答