sql普通语句
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;
另外+,-,*,/都可以使用
最新文章
- mysql 实现 start with
- javascript实现简单的轮播图片
- SQL匹配顺序
- 网站标题ico那些事
- window7部署solr 4.7
- Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp
- 第二百五十六天 how can I 坚持
- 20141015--for语句1
- angular中的orderBy过滤器使用
- 微信开发之开发环境搭建( visual studio 2015we + IIS express + ngrok)
- 谈一下spring 的理解
- aspnetpager分页,不使用存储过程
- ORA-12514(TNS:监听程序当前无法识别...)
- Web Design 再生:UX Design
- jquery getScript动态加载JS方法改进详解[转载]
- POJ 2601
- VS2010/2013 运行是很卡的加速方案
- TensorFlow分布式实践
- Android Intent Service
- 关于C++静态成员函数访问非静态成员变量的问题