4

我需要带有一些前缀和后缀的 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); 分隔的预订前缀;

请建议。

4

2 回答 2

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 ; 
于 2012-11-02T04:25:26.443 回答
1

我找到了我的解决方案:

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
于 2012-11-02T04:36:39.590 回答