sql 基本语法整理
2017-03-25 15:52
411 查看
1.创建表
create table t_user(menu varcha(30),password int(10));
create table 表名(字段名 字段类型(字段长度));
2.删除表
drop table t_user;
3.插入表
insert into t_user(menu,age) values('yy',10);
insert into 表名(字段名)values(值);
4.查询表
1)查询所有数据(*)
select * from t_user;
2)查询表的某一个或多个字段数据
select memu from t_user;
3)单条件查询
select menu from t_user where id=0;
4)多条件查询
select menu ,age from t_user where id=0 and account='xx';
select menu ,age from t_user where id>0 and id<5 ;
select menu ,age from t_user where id>0 or id<5 ;
实例时错误:Unknown column 'user_phone' in 'field list'(未知的字段名user_phone)
user_phone不存在与t_user这张表里(查询的字段不存在被查询的表中)
5)查询并排序数据
select menu,age from t_user
order by id asc;
asc顺序(默认) desc 倒序
6)查询总数数据
select count(1) as totalcount from t_p;
7)字段数据求和
select
sum(id) as sumvalue from t_user;
8)字段数据平均值
select avg(id) as avgvalue from t_user;
9)字段数据最大值
select max(id) as maxvalue from t_user;
10)字段数据最小值
select min(id) as minvalue from t_user;
11)多表查询数据
select t1.age,t2.accont from t_user as t1,t_test as t2 where t1.id=t2.name;
12)子查询
select account,id FROM( SELECT account ,id FROM t_p WHERE age<6 ) t1 WHERE t1. id>0 and t1.id<5;
注:t1是别名
13)左右链接查询
select t_user.id,t_test.password FROM t_user LEFT JOIN t_test ON t_user.id=t_test.password;
5.删除表数据
1)删除所有数据
delete from t_user;
2)单条件删除
delete from t_user where age=1;
3)多条件删除
delete from t_user where id=1 and account='xx';
delete from t_user where id>0 and id<5;
delete from t_user where id>0 or id<5;
6.修改表结构
1)添加字段
alter table t_user add height varchar(30);
2)添加主键
alter table t_user add primary key (id);
3)删除主键
alter table t_user drop primary key(id);
7.修改数据
update t_user set id=10 where id=6;
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
create table t_user(menu varcha(30),password int(10));
create table 表名(字段名 字段类型(字段长度));
2.删除表
drop table t_user;
3.插入表
insert into t_user(menu,age) values('yy',10);
insert into 表名(字段名)values(值);
4.查询表
1)查询所有数据(*)
select * from t_user;
2)查询表的某一个或多个字段数据
select memu from t_user;
3)单条件查询
select menu from t_user where id=0;
4)多条件查询
select menu ,age from t_user where id=0 and account='xx';
select menu ,age from t_user where id>0 and id<5 ;
select menu ,age from t_user where id>0 or id<5 ;
实例时错误:Unknown column 'user_phone' in 'field list'(未知的字段名user_phone)
user_phone不存在与t_user这张表里(查询的字段不存在被查询的表中)
5)查询并排序数据
select menu,age from t_user
order by id asc;
asc顺序(默认) desc 倒序
6)查询总数数据
select count(1) as totalcount from t_p;
7)字段数据求和
select
sum(id) as sumvalue from t_user;
8)字段数据平均值
select avg(id) as avgvalue from t_user;
9)字段数据最大值
select max(id) as maxvalue from t_user;
10)字段数据最小值
select min(id) as minvalue from t_user;
11)多表查询数据
select t1.age,t2.accont from t_user as t1,t_test as t2 where t1.id=t2.name;
12)子查询
select account,id FROM( SELECT account ,id FROM t_p WHERE age<6 ) t1 WHERE t1. id>0 and t1.id<5;
注:t1是别名
13)左右链接查询
select t_user.id,t_test.password FROM t_user LEFT JOIN t_test ON t_user.id=t_test.password;
5.删除表数据
1)删除所有数据
delete from t_user;
2)单条件删除
delete from t_user where age=1;
3)多条件删除
delete from t_user where id=1 and account='xx';
delete from t_user where id>0 and id<5;
delete from t_user where id>0 or id<5;
6.修改表结构
1)添加字段
alter table t_user add height varchar(30);
2)添加主键
alter table t_user add primary key (id);
3)删除主键
alter table t_user drop primary key(id);
7.修改数据
update t_user set id=10 where id=6;
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值