SQL语句之-计算字段
Posted bell1991
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句之-计算字段相关的知识,希望对你有一定的参考价值。
五、计算字段
1、拼接字段
mysql:使用函数concat
SqlServer:使用加号+
oracle:使用||
SELECT CONCAT(vend_name,\'(\',vend_country,\')\') FROM vendors ORDER BY vend_name
使用别名AS
SELECT CONCAT(vend_name,\'(\',vend_country,\')\') AS vend_title FROM vendors ORDER BY vend_name
2、算数计算:支持+、—、*、/,用()区分优先级
SELECT prod_id,quantity,item_price,
quantity*item_price AS expanded_price
FROM orderitems WHERE order_num = 20008
六、分组
1、过滤分组
WHERE过滤行,而HAVING过滤分组。
SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id;
SELECT cust_id,COUNT(*) AS num_orders FROM orders GROUP BY cust_id HAVING COUNT(*)>=2;
SELECT vend_id,prod_price,COUNT(*) AS num_prods FROM products WHERE prod_price >= 4 GROUP BY vend_id HAVING COUNT(*)>= 2;//结合where语句
SELECT order_num,COUNT(*) AS items FROM orderitems GROUP BY order_num HAVING COUNT(*)>=3 ORDER BY items//分组没有排序,必须用order by指定排序
以上是关于SQL语句之-计算字段的主要内容,如果未能解决你的问题,请参考以下文章
1、sql查询语句时怎么把几个字段拼接成一个字段?这几个字段是整型的。
Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取