Android SQLite SQL语句高级进阶
2017-02-20 20:59
447 查看
本篇博客使用《SQL必知必会》中的SQL知识讲解sql语句,并使用书中自带的SQLite数据库文件。
该数据库文件中共五张表:
本篇博客的sql语句均是操作的tysql数据库文件中的这五张表。
数据库文件下载
SELECT 语句:
WHERE子句操作符:
通配符
% 表示任何字符出现任意次数(可以匹配任何字符串)
_ 表示任何单个字符(可以匹配任何单个字符)
该数据库文件中共五张表:
本篇博客的sql语句均是操作的tysql数据库文件中的这五张表。
数据库文件下载
SELECT 语句:
SELECT prod_name FROM Products;--执行单列检索
prod_name --------------------- 8 inch teddy bear 12 inch teddy bear 18 inch teddy bear Fish bean bag toy Bird bean bag toy Rabbit bean bag toy Raggedy Ann King doll Queen doll
SELECT prod_id , prod_name FROM Products;--执行多列检索
prod_id prod_name ----------- ----------------------- BR01 8 inch teddy bear BR02 12 inch teddy bear BR03 18 inch teddy bear BNBG01 Fish bean bag toy BNBG02 Bird bean bag toy BNBG03 Rabbit bean bag toy RGAN01 Raggedy Ann RYL01 King doll RYL02 Queen doll
SELECT * FROM Products;--检索所有列得到的结果是表中的所有数据,这里就不贴出来了。
SELECT DISTINCT vend_id From Products;--执行去重检索
vend_id --------- BRS01 DLL01 FNG01
SELECT prod_name FROM Products LIMIT 5;--检索prod_name列的前五行数据
prod_name ------------------ 8 inch teddy bear 12 inch teddy bear 18 inch teddy bear Fish bean bag toy Bird bean bag toy
SELECT prod_name FROM Products LIMIT 5 OFFSET 2;--检索prod_name列的五行数据从第三行开始 --SELECT prod_name FROM Products LIMIT 2 , 5; --检索prod_name列的五行数据从第三行开始
prod_name --------------------- 18 inch teddy bear Fish bean bag toy Bird bean bag toy Rabbit bean bag toy Raggedy Ann
SELECT prod_name FROM Products ORDER BY prod_name ASC;--检索prod_name并按照prod_name升序排序
prod_name --------------------- 12 inch teddy bear 18 inch teddy bear 8 inch teddy bear Bird bean bag toy Fish bean bag toy King doll Queen doll Rabbit bean bag toy Raggedy Ann
SELECT prod_name , prod_price FROM Products ORDER BY prod_price DESC;--检索prod_name和prod_price并按照prod_price降序排序
prod_name prod_price -------------- -------------------- 18 inch teddy bear 11.99 King doll 9.49 Queen doll 9.49 12 inch teddy bear 8.99 8 inch teddy bear 5.99 Raggedy Ann 4.99 Fish bean bag toy 3.49 Bird bean bag toy 3.49 Rabbit bean bag toy 3.49
SELECT prod_name ,prod_price FROM Products ORDER BY prod_price DESC , prod_name ASC;--检索prod_name和prod_price按照prod_price降序prod_name升序排序
prod_name prod_price ------------- -------------------- 18 inch teddy bear 11.99 King doll 9.49 Queen doll 9.49 12 inch teddy bear 8.99 8 inch teddy bear 5.99 Raggedy Ann 4.99 Bird bean bag toy 3.49 Fish bean bag toy 3.49 Rabbit bean bag toy 3.49
WHERE子句操作符:
操作符 说明 ------- ---- = 等于 <> 不等于 != 不等于 < 小于 <= 小于等于 > 大于 >= 大于等于 BETWEEN 两值之间 IS NULL 为NULL值
SELECT prod_price FROM Products WHERE prod_price > 5;--检索prod_price大于5的所有prod_price
prod_price ------------ 5.99 8.99 11.99 9.49 9.49
SELECT prod_name , prod_price FROM Products WHERE prod_price BETWEEN 3.0 AND 3.5;--检索prod_price大于3.0小于3.5的所有prod_name和prod_price
prod_name prod_price ------------ ------------ Fish bean bag toy 3.49 Bird bean bag toy 3.49 Rabbit bean bag toy 3.49
SELECT cust_name FROM Customers WHERE cust_email IS NULL;--检索cust_email为NULL值的cust_name
cust_name ------------ Kids Place The Toy Store
SELECT cust_name , cust_email FROM Customers WHERE NOT cust_email IS NULL;--检索cust_email不为NULL值的cust_name和cust_email
cust_name cust_email ------------ ------------ Village Toys sales@villagetoys.com Fun4All jjones@fun4all.com Fun4All dstephens@fun4all.com
SELECT prod_name , prod_price FROM Products WHERE vend_id = 'DLL01' AND prod_price < 4;--检索vend_id为DLL01并且prod_price小于4的prod_name和prod_price
prod_name prod_price ------------ ----------- Fish bean bag toy 3.49 Bird bean bag toy 3.49 Rabbit bean bag toy 3.49
SELECT vend_id , prod_price FROM Products WHERE vend_id = 'FNG01' OR prod_price = 5.99;--检索vend_id为FNG01或者prod_price等于5.99的vend_id和prod_price
vend_id prod_price -------- ----------- BRS01 5.99 FNG01 9.49 FNG01 9.49
SELECT vend_id , prod_price FROM Products WHERE vend_id IN ('BRS01' , 'FNG01');--检索vend_id为BRS01或者FNG01的vend_id和prod_price --等同于SELECT vend_id , prod_price FROM Products WHERE vend_id = 'BRS01' OR vend_id = 'FNG01';
vend_id prod_price -------- ----------- BRS01 5.99 BRS01 8.99 BRS01 11.99 FNG01 9.49 FNG01 9.49
SELECT vend_id , prod_price FROM Products WHERE (vend_id = 'BRS01' OR vend_id = 'FNG01') AND prod_price > 9;--检索vend_id为BRS01或者FNG01并且prod_price大于9的vend_id和prod_price
vend_id prod_price ------- --------- BRS01 11.99 FNG01 9.49 FNG01 9.49SQL
通配符
% 表示任何字符出现任意次数(可以匹配任何字符串)
_ 表示任何单个字符(可以匹配任何单个字符)
SELECT prod_name , prod_price FROM Products WHERE prod_name LIKE 'R%';--检索以R开头的prod_name及其对应的prod_price
prod_name prod_price ----------- ---------- Rabbit bean bag toy 3.49 Raggedy Ann 4.99
SELECT cust_name , cust_email FROM Customers WHERE cust_email LIKE '%';--检索所有cust_email不为NULL的cust_name和cust_email --%特性:可以匹配除NULL之外的所有字符串(包括空串)
cust_name cust_email ----------- ----------- Village Toys sales@villagetoys.com Fun4All jjones@fun4all.com Fun4All dstephens@fun4all.com
SELECT prod_name FROM Products WHERE prod_name LIKE '__ inch teddy bear';--检索所有以inch teddy bear结尾并在其前有两个任意字符的prod_name --_能且仅能匹配一个字符,不能多亦不能少。
prod_name ------------ 12 inch teddy bear 18 inch teddy bear
SELECT prod_name FROM Products WHERE prod_name LIKE '% inch teddy bear';--检索所有以inch teddy bear结尾的prod_name
prod_name ------------ 8 inch teddy bear 12 inch teddy bear 18 inch teddy bear
相关文章推荐
- 必学高级SQL语句
- 菜鸟到高手:SQL开发进阶常用精妙语句
- SQL Server SQL高级查询语句小结
- SQL Server SQL高级查询语句小结
- T-SQL查询进阶--流程控制语句
- 高级sql语句
- T-SQL查询进阶--流程控制语句
- 15条Oracle数据库维护高级SQL语句
- sql高级查询语句
- T-SQL查询进阶--流程控制语句
- sql高级语句查询
- sql高级语句大全
- sql高级技巧几个有用的Sql语句
- SSIS高级转换任务—执行SQL语句
- SQL高级查询语句
- (Sql Server高级技巧)使用SQL语句实现备份与还原
- sql高级技巧几个有用的Sql语句
- 高级嵌套的查询sql语句
- (柯昌合)Android Sqlite 持久化框架。类似于hibernate的sqlite框架。不用写SQL语句,操作数据库
- 使用日期函数高级应用的两个SQL语句