mysql的sql语句
连接数据库
链接数据库命令:mysql -h localhost -u root -p
出现mysql> 连接成功
展示数据库
show database;
新建数据库
create database 数据库名字;
删除数据库
drop database 数据库名字;
选择数据库
use 数据库名字
展示表
show tables;
新建表
创建表 | create table t_xxx( |
---|
| id int(16) not null, |
| xxx varchar(255), |
| remark varchar(255)) |
| ; |
数据库sql语句
查
select * from 表名; |
---|
select id,cname from 表的名字; select 字段1,字段2 from 表的名字; |
select 字段1,字段2 from 表名 where 条件 |
select * from t_student where age >30 and sex = ‘女’;‘字符串’要用引号 |
模糊查询like | select * from 表名 where 字段1 like ‘张%’; |
---|
查出所有姓张的 | select * from t_student where sname like ‘张%’; |
查出所有以凡结尾的 | select * from t_student where sname like ‘%凡’; |
查出所有带小的 | select * from t_student where sname like ‘%小%’; |
增
插入 | insert into 表名(字段1,字段2) values (值1,值2,值3); |
---|
| insert into t_class (id,cname,teacher) values (3,‘测试班’,‘王老师’); |
改
| update 表名 set 字段名=值1,字段名=值2 where 条件; |
---|
| update t_class set teacher = ‘王老师’ where id = 3;表里所有id为3的改为王老师,where后限制条件 |
| update t_class set phone-‘1234’ where sname like ‘%小%’; |
删
| delete * from 表名 where 条件; |
---|
| delete * from t_class where id = 3; |
表关系(实现多表联查)
学生表的cid=班级表的id
查
| select * from 表名1 inner join 表名2 on 表名1.字段名 = 表名2.字段名; (*查询所有) |
---|
| select * from t_student inner join t_class on t_student.cid = t_class.id; |
在navicat中查询
点击查询,点击美化sql
关联(取数据有区别)
内关联/inner join /join
select 字段1,字段2,字段3 from 表 1 join 表2 on 表关系; |
---|
select t_student.id,t_student.sname,t_student.sex,t_class.name,t_class.teacher from t_student join t_class on t_student.cis = t_ class.id |
外关联
左关联(left join)
left join左边是左表,只显示左表 |
---|
select t_student.id,t_student.sname,t_student.sex,t_class.name,t_class.teacher from t_student left join t_class on t_student.cis = t_ class.id |
右关联(right join)
right join右边是左表,只显示右表 |
---|
select t_student.id,t_student.sname,t_student.sex,t_class.name,t_class.teacher from t_student right join t_class on t_student.cis = t_ class.id |