mysql函数使用-字符串处理

1、按地区统计数据

select q.psn,t.name,sum(q.quarantinePigCount) from 
(select RPAD(substring(pigSourceNo,1,2),6,'0') as psn,quarantinePigCount from quarantines where status <> -1 and 
createDate between '2012-05-01 00:00:00' and '2012-05-05 23:59:59'
) q,typed_datas t where q.psn=t.id group by q.psn,t.name

其中select RPAD(substring(pigSourceNo,1,2),6,'0')  包含两个内容

(1)MySQL 字符串截取函数:left(), right(), substring(), substring_index()。

substring从字符串的第 1 个字符位置开始取,只取 2 个字符。

mysql> select substring('510700', 1, 2);
+---------------------------------+
| substring('510700', 1, 2) |
+---------------------------------+
| 51                              |
+---------------------------------+

(2)Mysql函数中RPAD(str,len,padstr) 意思为:返回字符串str,右面用字符串padstr填补直到str是len个字符长。

mysql> select RPAD('51', 6, '0');
+---------------------------------+
| RPAD('51', 6, '0') |
+---------------------------------+
| 510000                              |
+---------------------------------+

类似的还有LPAD(str,len,padstr) 意思为:返回字符串str,左面用字符串padstr填补直到str是len个字符长。

2、数据库中两个字段拼接显示

mysql> SELECT p.batch,p.supplierName FROM purchase_order p;
+------------+--------------+
| batch      | supplierName |
+------------+--------------+
| 2000100001 | 测试供应     |
+------------+--------------+

如果将两个字段拼接需要用到 concat函数

mysql> SELECT concat(p.batch,',',p.supplierName) as test FROM purchase_order p;
+---------------------+
| test                |
+---------------------+
| 2000100001,测试供应 |
+---------------------+
 

猜你喜欢

转载自liuxi1024.iteye.com/blog/1544112