sql 基本语法整理

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 列名称 = 某值




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值