MySQL使用函数处理数据以及分组数据
- 在MySQL的SELECT语句中,可使用Concat()函数来拼接两个列:
select concat(vend_name, ' (', vend_country, ')') from vendors order by vend_name;
- 删除数据右侧多余的空格可以用RTrim()函数来完成:
select concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') from vendors order by vend_name;
(函数RTrim()可以去除右边的空格,函数LTrim()可以去除左边的空格,Trim()可以去除两边的空格) - 用AS关键字赋予别名:
select concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS vend_title from vendors order by vend_name;
- 执行算术计算:
select prod_id, quantity, item_price, quantity*item_price AS expanded_price from orderitems where order_num = 20005;
- select测试和实验函数:
select 3*2;
将返回6。select Trim('abc');
将返回abc。select now();
返回当前日期和时间。
常用的文本处理函数:
函 |
---|