1-order by
-1- 单列
-2- 多列 order by column1, column2, column3
-3- 位置select语句中的最后一条子句
-4- order by的列,不一定要选择哪些被检索,被显示的列,哪些没有出现的列也是可以的。
-5- 支持用位置序号代替(必须是select清单中,所以不支持未检索的)列名 order by 1, 3; #列名不清楚,如果数据库表修改也会出乱问题
-6- 指定排序方向desc(降序),asc(升序,默认) 多列是要分别说明
order by column1 desc, column2, column3 desc; # column1 降序 z-a, column3 降序, 默认升序,desc必须放在后面。
-7-a与A是否排序先后,要根据不同数据库系统,可以手动修改数据库系统设置
select name from eight order by id; // 最后,产生查询结果后,在排序。任意列。
2-where
操作符
= 等于
<> 不等于
!= 不等于 (有的不支持,access, access只支持<>)
<
<=
!<
>
>=
!> 不大于
between 值1 and 值2 #在指定的两个值之间
is null 为空值
where column1 <> 10;
where column1 <> ‘10’; #字符串的情况要加单引号
where column1 between 1 and 10;
where column1 is null;
组合where子语句
and(优先级高于or先执行)
or
where column1='1' or column2='2' and column3 >= 10; 小于10的也会出现,次序问题
where (column1='1' or column2='2') and column3 >= 10; 这样才没有小于10的
in指定条件范围(效率快,而且可以在括号中嵌套select语句)
where column1 in ('1', '2'); = where column1 = '1' or column1 = '2';
not in的相反(mysql不支持,NOT EXISTS 代替)
where (id, num) > (2,3) 等价于 where id > 2 or num > 3;
3-通配符
%代表任何字符出现任意次数(access用*)
_代表任何字符出现一次,匹配单个字符
select * from one where num like '%3%';
select * from one where num like '_3';
select * from one where num like 12;
虽然num是int类型,同样可以用like,可以用字符串去匹配,但是要用上通配符,就一定是否字符串,一定要加上单引号,像%12是会报错的。
放过来char类型也可以直接like 12; 数据库会自动将12转换成字符串之后再进行比较的。
[]集合 (微软的才支持)
like '[js]'; // 意思是字符串里包含有j或s的,只能匹配一个
like '[js]%'; // 意思是字符串里以j或s开头的字符串。 [^js]是取相反的意思,[!js]有些用!;
4-拼接字符串
select num+':(' + x + ',' + y + ')' as point from one; // 多少数据库用+表示连接,如将点的序号加上坐标形成新的列, 2:(232,332)
// 有些数据库系统使用||,而不是+;
MySQL对上面的都不支持。
MySql 使用concat函数
select concat (vend_name, '(', vend_country, ')') from vendors; // 参数任意长。
5-别名
列别名
表别名:from语句表名 as 别名, 别名可以用于select,where, group by等, or
注意:oracle 不支持 as关键字,也可以说省略了as。
别名可以缩短sql语句,允许单条中多次使用
6-计算* / + -
select id, quantity * price as expanded_price from items; // 这种不是交叉相乘,而是每一行中两个元素的相乘,结果行数不会发生变化
select
SQL必知必会 来自淘豆网m.daumloan.com转载请标明出处.