sql次级语句

select upper(n_id) from nrc_news;
select left(n_content,1) from nrc_news;
select len(n_content) from nrc_news;

left() 返回字符串左边的字符
len() 返回字符串的长度
lower() 转换小写
right() 返回字符串右边的字符
soundex() 返回字符串的SOUNDEX值
upper() 转换大写

select n_content from nrc_news where DATEPART(YY,n_publishtime)=2015;
select n_content from nrc_news where DATEPART(YYYY,n_publishtime)=2015;
数值处理函数:
abs() 返回一个数的绝对值
cos() 返回一个角度的余弦
exp() 返回一个数的指数值
pi() 返回圆周率
sin() 返回一个数的正弦
sqrt() 返回一个数的平方根
tan() 返回一个数的正切

select ABS(n_id) from nrc_news;
select cos(n_id) from nrc_news;
select exp(n_id) from nrc_news;
select pi() from nrc_news;
select SIN(n_id) from nrc_news;
select sqrt(n_id) from nrc_news;
select tan(n_id) from nrc_news;

汇总数据
AVG() 返回某列的平均值
COUNT() 返回某列的行数
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列之和
select avg(t_id) from nrc_news;
select count(t_id) from nrc_news;
select max(t_id) from nrc_news;
select min(t_id) from nrc_news;
select sum(t_id) from nrc_news;
注:
avg()只能用来确定特定数值列的平均值,而且列明必须作为函数参数给出,如果要获得多个列的平均值,必须用多个avg()
avg()函数忽略列值为NULL的行

select count(*) from nrc_news where t_id=10;

数据分组
select t_id,count(*)as number from nrc_news group by t_id;
注解:
上面的SELECT语句制定了两个列,t_id和number(计算字段).group by 子句会指示数据库按t_id排序并分组数据.这样会对每个t_id而不是整个表计算number
这样输出的就是每个t_id所对应的行的数量

select t_id,count(*)as number from nrc_news group by t_id having COUNT(*)>=2;
注:
HAVING 和 WHERE 的差别
where是在数据分组前进行过滤,having是在数据分组后进行过滤
共同使用的时候,where排除的值不包括在分组中,会影响having的计算

使用子查询
select * from nrc_news where t_id in(1,2);
select t_id from nrc_news where n_content like '%就%';

select * from nrc_news where t_id in(select t_id from nrc_news where n_content like '%就%');
注:
作为子查询的select语句只能查询单个列,企图检索多个列将返回错误
select r_id,r_content,n_id,(select t_id from nrc_news where nrc_news.n_id=nrc_review.n_id)as orders from nrc_review ;
select n_id,n_title,t_id,(select t_memo from nrc_type where nrc_type.t_id=nrc_news.t_id)as t_memo from nrc_news
上述子查询为重点内容。重点记忆。
select top 10 n_id as id,
(select n_title from nrc_news where nrc_news.n_id=nrc_review.n_id)as title,
(select n_content from nrc_news where nrc_news.n_id=nrc_review.n_id)as content,
(select t_id from nrc_news where nrc_news.n_id=nrc_review.n_id)as t_id,
(select n_publishtime from nrc_news where nrc_news.n_id=nrc_review.n_id)as publishtime,
COUNT(*)as number from nrc_review group by n_id order by number desc,id;

原文地址:https://www.cnblogs.com/daochong/p/4869815.html