select DISTINCT t_id from nrc_news
DISTINCT不会输出相同的值
select top 5 * from nrc_news;
检索前五行
select * from nrc_news order by t_id;
通过子列t_id排序
注:指定一条order by子句时,应该保证它是select语句最后一条子句,如果不是,会报错
select * from nrc_news order by t_id,n_publishtime;
select * from nrc_news order by 4,5;
使用两条规则进行排序
select * from nrc_news order by t_id DESC;
select * from nrc_news order by t_id ASC;
降序和升序
select * from nrc_news order by t_id DESC,n_publishtime;
最大的t_id在前面然后按时间排序
select * from nrc_news where t_id=10;
过滤条件t_id=10
where操作符
= 等于
<> 不等于
!= 不等于
< 小于
<= 小于等于
!< 不小于
> 大于
>= 大于等于
!> 不大于
BETWEEN 在指定的两个值之间
IS NULL 为NULL值
例:
select * from nrc_news where t_id <> 10;
!=和<>通常可以互换。但是,并非所有DBMS都支持这两种不等于操作符。例如Microsoft Access支持<>不支持!=
select * from nrc_news where t_id between 3 and 9;
select * from nrc_news where t_id is null;
select * from nrc_news where t_id between 3 and 9 and n_id<> 3;
select * from nrc_news where t_id between 3 and 9 or n_id<> 3;
SQL语句中
and的处理是优先于or的处理的
select * from nrc_news where n_id>8 or n_id<2 and t_id<10;
select * from nrc_news where (n_id>8 or n_id<2) and t_id<10;
select * from nrc_news where t_id IN (1,2,3,4);
等于
select * from nrc_news where t_id=1 or t_id=2 or t_id=3 or t_id=4;
NOT可以否定后续的判断
select * from nrc_news where not t_id=1;
使用通配符进行判断:
select * from nrc_news where n_content like '%就%';
select * from nrc_news where n_content like '小%';
select * from nrc_news where n_content like '%了';
注:请注意NUll
where t_id like '%'不会匹配为NULL的行
select * from nrc_news where n_title like '梦_';
select * from nrc_news where n_title like '[梦喷]%';
select * from nrc_news where n_title like '[^梦喷]%';//除了梦和喷之外所有的开头
拼接字段:
select n_title+'('+n_content+')' from nrc_news ;
注:TRIM函数
大多数DBMS都支持RTRIM()(去掉字符串右边的空格),LTRIM()(去掉字符串左边的空格),TRIM()(去掉字符串两边的空格)函数
select n_id,n_title,n_content,n_id*t_id as number from nrc_news order by number;
另外+,-,*,/都可以使用