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

SQL必知必会摘要

时间:2017-05-29 00:59:39      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:必知必会   where   大小写   空值   检索   限制   first   name   用法   

数据检索 2.2 检索单个列 SELECT prod_name FROM Products; SQL语句不区分大小写
  2.3 检索多个列 SELECT prod_nameprod_idprod_price FROM Products;  
  2.4 检索所有列 SELECT * FROM Products;  
  2.5 检索不同的值 SELECT DISTINCT vend_id FROM Products;  
  2.6 限制结果 SELECT TOP 5 prod_name FROM Products;(SQL server、Access)
SELECT prod_name FROM Products
FETCH FIRST 5 ROWS ONLY;(DB2)
SELECT prod_name FROM Products
WHERE ROWNUM <=5;(Oracle)
SELECT prod_name FROM Products
LIMIT 5;(MySQL、SQLite等)
SELECT prod_name FROM Products LIMIT 4 OFFSET 3;(LIMIT 3,4)
从第3行起的4行数据
  2.7 使用注释 SELECT prod_name --这是一条注释(--之后的是注释)
FROM Products;
# 这是一条注释                  (单行注释)
/*这是一条注释,很长           
(跨行注释,通常用于代码注释)
很长的注释*/
 
排序检索数据 3.1 排序数据 SELECT prod_name FROM Products ORDER BY prod_name; 字母升序拍;可以通过非检索列进行排序
  3.2 按多个列排序 SELECT prod_name,prod_id,prod_price FROM Products
ORDER BY prod_price,prod_name;
prod_price相同时才按照prod_name
  3.3 按列位置排序 SELECT prod_name,prod_id,prod_price FROM Products
ORDER BY 2,3;
表示先按prod_id,再按prod_price排序
  3.4 指定排序方向 SELECT prod_name,prod_id,prod_price FROM Products
ORDER BY prod_price DESC;
降序排列,大小写字母在排序时是否区分取决于数据库的配置
    SELECT prod_name,prod_id,prod_price FROM Products
ORDER BY prod_price DESC,prod_name;
prod_name仍为升序,ASC表升序(默认)
过滤数据 4.1 使用WHERR SELECT prod_name,prod_price FROM Products
WHERE prod_price = 3.49;
 
    SELECT prod_name,prod_price FROM Products
WHERE prod_price = 3.49
ORDER BY prod_name;
WHERE和ORDER BY组合用法
  4.2 WHERE子句操作符 =,<>,!=,<,<=,!<,>,>=,!>,BETWEEN,IS NULL  
  4.2.2 不匹配检查 SELECT  vend_id,prod_name FROM Products
WHERE vend_id<>‘DLL01‘;
不匹配检查,字符串需要加引号<>与!=等价
  4.2.3 范围值检查 SELECT prod_name,prod_price FROM Products
WHERE prod_price BETWEEN 5 AND 10
包括5和10
  4.2.4 空值检查 SELECT prod_name,prod_price FROM Products
WHERE prod_price IS NULL;
NULL与非匹配:空值不会被返回
高级数据过滤 5.1.1 AND操作符 SELECT prod_id,prod_price,prod_name FROM Products
WHERE vend_id = ‘DLL01‘
AND prod_price<=4;
可用多个AND,增加过滤条件
  5.1.2 OR操作符 SELECT prod_id,prod_price,prod_name FROM Products
WHERE vend_id = ‘DLL01‘
OR vend_id = ‘BRS01‘;
第一个条件满足时,不管第二个条件是否满足,相应行都被检索出来
    SELECT prod_id,prod_price,prod_name FROM Products
WHERE (vend_id = ‘DLL01‘
OR vend_id = ‘BRS01‘)AND prod_price<=10;
组合使用,使用圆括号。AND优先级较高
  5.2 IN操作符 SELECT prod_name,prod_price FROM Products
WHERE vend_id =
IN( ‘DLL01‘,‘BRS01‘)
ORDER BY prod_name;
功能与OR相当
    SELECT prod_name,prod_price FROM Products
WHERE vend_id = ‘DLL01‘
OR vend_id = ‘BRS01‘
ORDER BY prod_name;
改写IN语句
  5.3 NOT操作符 SELECT prod_name FROM Products
WHERE
NOT vend_id = ‘DLL01‘
ORDER BY prod_name;
 
    SELECT prod_name FROM Products
WHERE vend_id
<> ‘DLL01‘
ORDER BY prod_name;
改写NOT语句
用通配符进行过滤 6.1.1 %通配符 SELECT prod_id,prod_name FROM Products
WHERE prod_name
LIKE ‘Fish%‘;
找出以Fish起头的词,Fish之后可以是0个字符;Acess需要使用*非%;大小写是否区分取决于数据库配置
    SELECT prod_id,prod_name FROM Products
WHERE prod_name
LIKE%bean bag%‘;
匹配任何包含bean bag的值
    SELECT prod_name FROM Products
WHERE prod_name
LIKE ‘F%y‘;
匹配F开头,y结尾的值(b%@forta.com)
%匹配0个、1个、多个字符
如果字符末尾是空格,则需要用F%y%来匹配
    WHERE prod_name LIKE ‘%‘; 不配匹配prod_name为NULL的行
  6.1.2 _通配符 SELECT prod_id,prod_name FROM Products
WHERE prod_name
LIKE ‘__ inch teddy bear‘;
DB2不支持,Access用?非_
_只匹配单个字符,功能与%通,如需匹配2个字符,则需要使用__

SQL必知必会摘要

标签:必知必会   where   大小写   空值   检索   限制   first   name   用法   

原文地址:http://www.cnblogs.com/hotpsy/p/6917071.html

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