序言: 嵌入式数据库Sqlite的基本sql使用汇总,使用测试起来,与关系型数据库mysql在语法上有很多的相似之处,先准备测试数据:
CREATE TABLE COMPANY(ID INT NOT NULL, NAME VARCHAR(20),AGE INT,ADDRESS VARCHAR(20),SALARY DECIMAL(7,2));
INSERT INTO "COMPANY" VALUES(1,'Paul',32,'California',20000);
INSERT INTO "COMPANY" VALUES(2,'Allen',25,'Texas',15000);
INSERT INTO "COMPANY" VALUES(3,'Teddy',23,'Norway',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',25,'Rich-Mond',65000);
INSERT INTO "COMPANY" VALUES(5,'David',27,'Texas',85000);
INSERT INTO "COMPANY" VALUES(6,'Kim',22,'South-Hall',45000);
INSERT INTO "COMPANY" VALUES(7,'James',24,NULL,10000);
INSERT INTO "COMPANY" VALUES(8,'Xiaoteng',29,NULL,NULL);
1,分组统计排序
GROUP BY 进行分组统计数据,命令如下:
sqlite> SELECT NAME, SUM(SALARY) SALARY_SUM, COUNT(1) COUNT_NUM FROM COMPANY GROUP BY NAME;
ORDER BY 进行排序,命令如下:
sqlite> SELECT NAME, SUM(SALARY) SALARY_SUM, COUNT(1) COUNT_NUM FROM COMPANY GROUP BY NAME ORDER BY SALARY_SUM ASC;
HAVING 字句过滤数据记录,命令如下:
SELECT c.*,COUNT(1) COUNT_NUM FROM COMPANY c GROUP BY c.NAME HAVING (COUNT_NUM) > 1 ORDER BY COUNT_NUM ;
PS:在一个查询中,HAVING 子句必须放在 GROUP BY 子句之后,必须放在 ORDER BY 子句之前。下面是包含 HAVING 子句的 SELECT 语句的语法:
2,Limit分页统计语句
SQLite 的 LIMIT 子句用于限制由 SELECT 语句返回的数据数量。
第一页取值SQL: SELECT * FROM COMPANY ORDER BY ID LIMIT 0,3; 也可以从一个特定的偏移开始提取记录,从第四位开始提取 3 个记录,使用OFFSET关键字,SELECT * FROM COMPANY ORDER BY ID LIMIT 3 OFFSET 0;PS:英国威廉希尔公司网站
从0开始取值。
第二页取值SQL: SELECT * FROM COMPANY ORDER BY ID LIMIT 3,3; 也可以从一个特定的偏移开始提取记录,从第四位开始提取 3 个记录,使用OFFSET关键字,SELECT * FROM COMPANY ORDER BY ID LIMIT 3 OFFSET 3;如下图所示:
问题地址:http://bbs.csdn.net/topics/390886865
sqlite> CREATE TABLE t1(id int, name varchar(60));
sqlite> INSERT INTO "t1" VALUES(4,'1@test.cn');
sqlite> select * from t1;
id name
---------- ----------
4 1@test.cn
sqlite> update t1 set name=(id/2)||substr(name,instr(name,'@'),length(name)-instr(name,'@')+1) where id=4;
sqlite> select * from t1;
id name
---------- ----------
4 2@test.cn
sqlite>
6,对Null值的处理
往表里面录入Null值
sqlite> INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES(8,'Xiaoteng',29,NULL,18000);
sqlite>
修改某个字段为null值
sqlite> UPDATE COMPANY SET SALARY = NULL WHERE ID=8;
sqlite>
查询为null的记录
sqlite> SELECT * FROM COMPANY WHERE ADDRESS IS NULL;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
7 James 24 10000
8 Xiaoteng 29
sqlite>
查询不为null的记录
sqlite> SELECT * FROM COMPANY WHERE ADDRESS IS NOT NULL;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
sqlite>
7,子查询
SELECt中的基本语法如下:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
实例如下:
sqlite> SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY WHERE SALARY > 45000);
INSERT语句中的子查询使用,基本语法:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
实例如下:
sqlite> INSERT INTO COMPANY_BKP
SELECT * FROM COMPANY
WHERE ID IN (SELECT ID
FROM COMPANY) ;
UPDATE语句中的子查询使用,基本语法如下:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
实例如下:
sqlite> UPDATE COMPANY
SET SALARY = SALARY * 0.50
WHERE AGE IN (SELECT AGE FROM COMPANY_BKP
WHERE AGE >= 27 );
DELETE语句中的子查询使用,语法如下:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
实例如下:
sqlite> DELETE FROM COMPANY
WHERE AGE IN (SELECT AGE FROM COMPANY_BKP
WHERE AGE > 27 );
录入测试数据
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE COMPANY(ID INT NOT NULL, NAME VARCHAR(20),AGE INT,ADDRESS VARCHAR(20),SALARY DECIMAL(7,2));
INSERT INTO "COMPANY" VALUES(2,'Allen',25,'Texas',15000);
INSERT INTO "COMPANY" VALUES(3,'Teddy',23,'Norway',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',25,'Rich-Mond',65000);
INSERT INTO "COMPANY" VALUES(5,'David',27,'Texas',85000);
INSERT INTO "COMPANY" VALUES(6,'Kim',22,'South-Hall',45000);
INSERT INTO "COMPANY" VALUES(7,'James',24,'Houston',10000);
INSERT INTO "COMPANY" VALUES(7,'James',28,'Houston',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',29,'Rich-Mond',95000);
COMMIT;
sqlite>
查看重复记录数
sqlite> select * from company order by name;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
2 Allen 25 Texas 15000
5 David 27 Texas 85000
7 James 24 Houston 10000
7 James 28 Houston 20000
6 Kim 22 South-Hall 45000
4 Mark 25 Rich-Mond 65000
4 Mark 29 Rich-Mond 95000
3 Teddy 23 Norway 20000
sqlite>
通过rowid来删除重复记录
sqlite> DELETE FROM COMPANY WHERE rowid NOT IN(SELECT MAX(rowid) rowid FROM COMPANY GROUP BY NAME);
sqlite>
再查看最新的数据记录,已经删除了重复NAME的记录
sqlite> select * from company;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 28 Houston 20000
4 Mark 29 Rich-Mond 95000
sqlite>
序言: 嵌入式数据库Sqlite的基本sql使用汇总,使用测试起来,与关系型数据库mysql在语法上有很多的相似之处,先准备测试数据:
CREATE TABLE COMPANY(ID INT NOT NULL, NAME VARCHAR(20),AGE INT,ADDRESS VARCHAR(20),SALARY DECIMAL(7,2));
INSERT INTO "COMPANY" VALUES(1,'Paul',32,'California',20000);
INSERT INTO "COMPANY" VALUES(2,'Allen',25,'Texas',15000);
INSERT INTO "COMPANY" VALUES(3,'Teddy',23,'Norway',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',25,'Rich-Mond',65000);
INSERT INTO "COMPANY" VALUES(5,'David',27,'Texas',85000);
INSERT INTO "COMPANY" VALUES(6,'Kim',22,'South-Hall',45000);
INSERT INTO "COMPANY" VALUES(7,'James',24,NULL,10000);
INSERT INTO "COMPANY" VALUES(8,'Xiaoteng',29,NULL,NULL);
1,分组统计排序
GROUP BY 进行分组统计数据,命令如下:
sqlite> SELECT NAME, SUM(SALARY) SALARY_SUM, COUNT(1) COUNT_NUM FROM COMPANY GROUP BY NAME;
ORDER BY 进行排序,命令如下:
sqlite> SELECT NAME, SUM(SALARY) SALARY_SUM, COUNT(1) COUNT_NUM FROM COMPANY GROUP BY NAME ORDER BY SALARY_SUM ASC;
HAVING 字句过滤数据记录,命令如下:
SELECT c.*,COUNT(1) COUNT_NUM FROM COMPANY c GROUP BY c.NAME HAVING (COUNT_NUM) > 1 ORDER BY COUNT_NUM ;
PS:在一个查询中,HAVING 子句必须放在 GROUP BY 子句之后,必须放在 ORDER BY 子句之前。下面是包含 HAVING 子句的 SELECT 语句的语法:
2,Limit分页统计语句
SQLite 的 LIMIT 子句用于限制由 SELECT 语句返回的数据数量。
第一页取值SQL: SELECT * FROM COMPANY ORDER BY ID LIMIT 0,3; 也可以从一个特定的偏移开始提取记录,从第四位开始提取 3 个记录,使用OFFSET关键字,SELECT * FROM COMPANY ORDER BY ID LIMIT 3 OFFSET 0;PS:英国威廉希尔公司网站
从0开始取值。
第二页取值SQL: SELECT * FROM COMPANY ORDER BY ID LIMIT 3,3; 也可以从一个特定的偏移开始提取记录,从第四位开始提取 3 个记录,使用OFFSET关键字,SELECT * FROM COMPANY ORDER BY ID LIMIT 3 OFFSET 3;如下图所示:
问题地址:http://bbs.csdn.net/topics/390886865
sqlite> CREATE TABLE t1(id int, name varchar(60));
sqlite> INSERT INTO "t1" VALUES(4,'1@test.cn');
sqlite> select * from t1;
id name
---------- ----------
4 1@test.cn
sqlite> update t1 set name=(id/2)||substr(name,instr(name,'@'),length(name)-instr(name,'@')+1) where id=4;
sqlite> select * from t1;
id name
---------- ----------
4 2@test.cn
sqlite>
6,对Null值的处理
往表里面录入Null值
sqlite> INSERT INTO COMPANY(ID,NAME,AGE,ADDRESS,SALARY) VALUES(8,'Xiaoteng',29,NULL,18000);
sqlite>
修改某个字段为null值
sqlite> UPDATE COMPANY SET SALARY = NULL WHERE ID=8;
sqlite>
查询为null的记录
sqlite> SELECT * FROM COMPANY WHERE ADDRESS IS NULL;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
7 James 24 10000
8 Xiaoteng 29
sqlite>
查询不为null的记录
sqlite> SELECT * FROM COMPANY WHERE ADDRESS IS NOT NULL;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
sqlite>
7,子查询
SELECt中的基本语法如下:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
实例如下:
sqlite> SELECT * FROM COMPANY WHERE ID IN (SELECT ID FROM COMPANY WHERE SALARY > 45000);
INSERT语句中的子查询使用,基本语法:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
实例如下:
sqlite> INSERT INTO COMPANY_BKP
SELECT * FROM COMPANY
WHERE ID IN (SELECT ID
FROM COMPANY) ;
UPDATE语句中的子查询使用,基本语法如下:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
实例如下:
sqlite> UPDATE COMPANY
SET SALARY = SALARY * 0.50
WHERE AGE IN (SELECT AGE FROM COMPANY_BKP
WHERE AGE >= 27 );
DELETE语句中的子查询使用,语法如下:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
实例如下:
sqlite> DELETE FROM COMPANY
WHERE AGE IN (SELECT AGE FROM COMPANY_BKP
WHERE AGE > 27 );
录入测试数据
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE COMPANY(ID INT NOT NULL, NAME VARCHAR(20),AGE INT,ADDRESS VARCHAR(20),SALARY DECIMAL(7,2));
INSERT INTO "COMPANY" VALUES(2,'Allen',25,'Texas',15000);
INSERT INTO "COMPANY" VALUES(3,'Teddy',23,'Norway',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',25,'Rich-Mond',65000);
INSERT INTO "COMPANY" VALUES(5,'David',27,'Texas',85000);
INSERT INTO "COMPANY" VALUES(6,'Kim',22,'South-Hall',45000);
INSERT INTO "COMPANY" VALUES(7,'James',24,'Houston',10000);
INSERT INTO "COMPANY" VALUES(7,'James',28,'Houston',20000);
INSERT INTO "COMPANY" VALUES(4,'Mark',29,'Rich-Mond',95000);
COMMIT;
sqlite>
查看重复记录数
sqlite> select * from company order by name;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
2 Allen 25 Texas 15000
5 David 27 Texas 85000
7 James 24 Houston 10000
7 James 28 Houston 20000
6 Kim 22 South-Hall 45000
4 Mark 25 Rich-Mond 65000
4 Mark 29 Rich-Mond 95000
3 Teddy 23 Norway 20000
sqlite>
通过rowid来删除重复记录
sqlite> DELETE FROM COMPANY WHERE rowid NOT IN(SELECT MAX(rowid) rowid FROM COMPANY GROUP BY NAME);
sqlite>
再查看最新的数据记录,已经删除了重复NAME的记录
sqlite> select * from company;
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
5 David 27 Texas 85000
6 Kim 22 South-Hall 45000
7 James 28 Houston 20000
4 Mark 29 Rich-Mond 95000
sqlite>