注:该笔记摘自MySQL必知必会,若侵权即删
MySQL计算字段
1.字段拼接(使用Concat关键字)
例子:
SELECT Concat(colunmn_name1, ‘(’ , column_name2, ‘)’ )
FROM mytable
ORDER BY column_name1;
特殊:可以删除数据多余的空格(使用RTrim删除右侧多余空格、LTrim删除左侧多余空格)
SELECT Concat(RTrim(colunmn_name1), ‘(’ , RTrim(column_name2), ‘)’ )
2.字段别名(使用AS关键字)
例子:
SELECT Concat(colunmn_name1, ‘(’ , column_name2, ‘)’ ) AS new_name1
FROM mytable
ORDER BY column_name1;
3.字段计算(直接使用运算符,前提条件:数据类型是数值类的)
例子:支持加减乘除
SELECT column_name1*column_name2 AS new_name1
FROM mytable
ORDER BY column_name1;