为sql结果添加前缀

时间:2016-06-03 18:06:56

标签: mysql sql

我试图在字段中添加前缀(ji.issuenum,见下文),即十进制(18,0)。所以' 4'成为' PROJECT-4' 正如我从示例中发现的那样,它足以用+:

添加它
SELECT 'PROJECT-' + ji.issuenum AS project_id,
       c.cname                  AS component_name,
       pri.pname                AS issue_priority,
       ist.pname                AS issue_status,
       ji.summary               AS issue_summary,
       ji.created               AS created,
       ji.updated               AS updated,
       ji.resolutiondate        AS resolutiondate
FROM   project p
       left outer join jiraissue ji
                    ON ji.project = p.id
       left outer join priority pri
                    ON ji.priority = pri.id
       left outer join issuestatus ist
                    ON ji.issuestatus = ist.id
       left outer join component c
                    ON c.project = p.id
WHERE  p.pname = 'Project'
       AND Year(resolutiondate) = Year(current_date - interval 1 month)
       AND Month(resolutiondate) = Month(current_date - interval 1 month)
GROUP  BY ji.issuenum;  

但它不起作用。 在加入之前我也尝试将它转换为char:

SELECT 'PROJECT-' + Cast( ji.issuenum AS CHAR(20)) AS project_id,
       c.cname                                     AS component_name,
       pri.pname                                   AS issue_priority,
       ist.pname                                   AS issue_status,
       ji.summary                                  AS issue_summary,
       ji.created                                  AS created,
       ji.updated                                  AS updated,
       ji.resolutiondate                           AS resolutiondate
FROM   project p
       left outer join jiraissue ji
                    ON ji.project = p.id
       left outer join priority pri
                    ON ji.priority = pri.id
       left outer join issuestatus ist
                    ON ji.issuestatus = ist.id
       left outer join component c
                    ON c.project = p.id
WHERE  p.pname = 'Project'
       AND Year(resolutiondate) = Year(current_date - interval 1 month)
       AND Month(resolutiondate) = Month(current_date - interval 1 month)
GROUP  BY ji.issuenum;  

还有其他办法吗?

由于

1 个答案:

答案 0 :(得分:3)

在mysql中使用CONCAT很可能会解决您的问题。它设置为使用字符串作为输入然后连接它们,但如果包含数值,它将把这些值转换为非二进制字符串等价物。这适用于整数和小数。

CONCAT('PROJECT-',TRUNCATE(ji.issuenum, 0)) AS Project_ID

我在查询中添加了TRUNCATE,因为我意识到如果ji.issuenum的小数值为4.0,你仍会得到'PROJECT-4.0'。 CONCAT不会舍入或修剪它只是“按原样”显示它们的值,即使它是等效的字符串。截断(...,0)将在小数点右侧显示0位数。

Here是一些针对CONCAT的mysql特定文档。