将字符串添加到mysql结果中

时间:2012-11-02 04:21:24

标签: mysql string add

我需要带有一些前缀和后缀的mySql结果。这是我的代码:

SELECT bk_id, bk_rtype, villas_db.v_name AS villa_name
FROM booking_db
INNER JOIN villas_db ON booking_db.bk_vid = villas_db.v_id
WHERE '2012-11-02'
BETWEEN bk_date1
AND bk_date2
ORDER BY bk_id DESC
LIMIT 0 , 30

结果是:

bk_id   bk_rtype    villa_name
30          2           T2
29          3           V1

所以我需要结果:

bk_id   bk_rtype    villa_name     booking_no
30          2           T2         B2-00030
29          3           V1         B3-00029

B是由“ - ”和sprintf(“%05d”,bk_id)分隔的预订前缀;

请建议。

2 个答案:

答案 0 :(得分:6)

您可能希望使用CONCATLPAD功能,如下所示:

 SELECT bk_id, bk_rtype, villas_db.v_name AS villa_name, 
        CONCAT('B',bk_rtype,'-', LPAD(bk_id, 5, '0')) AS booking_no
 FROM booking_db
 INNER JOIN villas_db ON booking_db.bk_vid = villas_db.v_id
 WHERE '2012-11-02'
     BETWEEN bk_date1
     AND bk_date2
 ORDER BY bk_id DESC 
 LIMIT 0 , 30 ; 

答案 1 :(得分:0)

我找到了解决方案:

SELECT bk_id, bk_rtype, villas_db.v_name AS villa_name, concat( 'B', bk_rtype, '-', lpad( bk_id, 5, 0 ) ) AS booking_no
FROM booking_db
INNER JOIN villas_db ON booking_db.bk_vid = villas_db.v_id
WHERE '2012-11-02'
BETWEEN bk_date1
AND bk_date2
ORDER BY bk_id DESC
LIMIT 0 , 30