基本的SQL语法:
(1)数据记录筛选:
select * from 表名
sql="select * from 数据表 where 字段名=字段值 order by 字段名 "
sql="select * from 数据表 where 字段名 like ‘%字段值%‘ order by 字段名 "
sql="select top 10 * from 数据表 where 字段名 order by 字段名 "
sql="select * from 数据表 where 字段名 in (‘值1‘,‘值2‘,‘值3‘)"
sql="select * from 数据表 where 字段名 between 值1 and 值2"
select*from 数据表order by 字段名 desclimit 15 :将字段名按降序排列,取出前15条数据
Select a.peer_id from table1a,table2 b 打开两张表时,可以给一个别名
wherea.peer_id=b.peer_id and online_info=1 and priority=0 条件用and连接
orderby register_time asc 表示升序排列 desc为降序
limit4; 表示只输出4条
limit2,4; 表示从第三条开始取4条
(2) 更新数据记录:
update 表名set 更新的数据 where 条件
sql="update 数据表set 字段名=字段值 where 条件表达式"
sql="update 数据表set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
Update table set weight=weight+1where peer_id in (‘12’,’14’,’25’,’31’);
Update table set weight=weight+1
update food setunitName=f.unitName
fromfood d ,(select * from food )as f where d.fid = f.fid;
(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
(4) 添加数据记录:
insert into 表名values("1991.3","张三","21","男")
insert into table1(field1,field2)values(value1,value2)
sql="insert into 数据表 (字段1,字段2,字段3 …) valuess (值1,值2,值3 …)"
sql="insert into 目标数据表 select* from 源数据表" (把源数据表的记录添加到目标数据表)
(5) 数据记录统计函数:
总数:selectcount as totalcount from table1
求和:selectsum(field1) as sumvalue from table1
平均:selectavg(field1) as avgvalue from table1
最大:selectmax(field1) as maxvalue from table1
最小:selectmin(field1) as minvalue from table1
AVG(字段名)得出一个表格栏平均值
COUNT(*|字段名)对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名)取得一个表格栏最大的值
MIN(字段名)取得一个表格栏最小的值
SUM(字段名)把数据栏的值相加
(6) 其他:
1. 显示数据库表的结构 describe table_name;
2. 显示表中的记录 select * form tabl_name;
3. 创建数据库 creae database database_name;
4. 打印表 show tables;
5. 显示表结构 describe 表名;
6. 建表
drop database if exists mldn;
create database mldn;
use mldn;
drop table if exists emp;
create table emp(
empno int(4) primarykey,
ename varchar(10),
job varchar(9),
hiredate date,
sal float(7.2)
);
(7) 嵌套使用方法:
在linux上运行MySQL的步骤:
1. 打开数据库 mysql –u[用户名] –p[密码]
2. Use mysql
3.
Sqlite 和 MySQL的区别:
SQLite:
1. Sqlite是轻量级数据库,不需安装
2. sqlite 不能并发操作
3. 每1MB的数据库文件需要256字节缓存页
4. 存在延时执行效率不高
5. 安装方便(只有一个文件)
6. 适合只是简单的测试应用
7. 适合快速开发
8. 适合嵌入式开发
9. 没有用户管理功能
10. 没有完整的数据库功能
MySQL:
1. 比SQLite复杂,也比其难装
2. 适合做产品
3. 有管理用户权限等功能
4. 有完整的数据库功能