码迷,mamicode.com
首页 > 数据库 > 详细

sql普通语句

时间:2015-10-11 16:33:32      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

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;

另外+,-,*,/都可以使用

sql普通语句

标签:

原文地址:http://www.cnblogs.com/daochong/p/4869388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!