当前位置:Gxlcms > 数据库问题 > mysql常用操作

mysql常用操作

时间:2021-07-01 10:21:17 帮助过:1人阅读

 show create table students;  查看student表结构

   flush privileges 刷新数据库

4.远程登录mysql

 mysql  -h  主机地址   -u用户名 -p用户密码   例如:mysql -h 192.168.1.119 -u root -p123456    密码和-p之间不能有空格 

  退出mysql命令:exit

3.插入数据insert语句

  (1)insert中不指定字段插入    

  insert into students values  (801,‘刘海洋‘,‘男‘,21,‘aa‘,‘北京市海淀区‘);

 (2)insert中指定字段插入

  insert into students (name,sex,age,class) values  (801,‘刘海洋‘,‘男‘,21,‘aa‘);

 (3)inser into 表名1 (属性列表1)  select  属性列表2 from表名2 where 条件表达式;

   insert into new_student select * from students;

 (4)replace插入新记录  

  replace语句的语法格式有三种语法格式。

  语法格式1: replace into 表名 [(字段列表) ] values (值列表)

  语法格式2: replace [into] 目标表名[(字段列表1)]  select (字段列表2) from 源表 where 条件表达式  

  语法格式3:replace [into] 表名set 字段1=值1, 字段2=值2

  例如:replace  into students values  (801,‘刘海洋‘,‘男‘,21,‘aa‘,‘北京市海淀区‘);

3.修改数据update语句

  update 表名

  set 属性名1=取值1, 属性名2=取值2,…,属性名n=取值n

  where 条件表达式;

  例:update students  set name=‘花花‘  where  age=18;

4.删除操作delete语句

  delete from 表名    where  条件表达式;

  例:delete from students  where age =18;

5.查询操作select语句  

  select
  {*|<字段列表>}
  [
  from <表1>,<表2>...
  [where <表达式>]
  [group by 字段名]
  [having 表达式]
  [order by 字段名]
  [limit [<offset>,]<row count>]
  ]
  select [字段1,字段2,...,字段n]
  from [表或视图]
  where [查询条件]

(1)条件查询   条件判断符=,<>,!=,<,<=,>,>=,between...and(位于两者之间)     is null、  is not null    in not in  

  select * from students  where age between 18 and 25;#查询年龄在18-25岁之间的学生信息

  select * from students  where  addr is null or addr=‘‘;#查询地址为null或者为‘‘的学生信息

      select * from students  where  id in(801,802)#查询id在801、802的学生信息

  带like的字符匹配查询   1)百分号通配符‘ %‘,匹配任意长度的字符,甚至包括零字符   2)下划线通配符‘_‘,一次只能匹配任意一个字符

  select * from students where name like ‘李_‘; #查询姓李名字为2个字的学生信息
  select * from students where name like ‘张%;#查询姓张的学生的信息
  select * from students where name like ‘%三%‘;#查询名字中含有‘三‘的学生信息 

(2)分组

  select * from students group by class;#以calss分组

  聚合函数:sum(),count(),avg(),max(),min()

  select avg(grade) from score; #查询score表平均分数   select sum(grade) from score;#查询score表分数总和   select min(grade) from score;#查询score表中分数最低的记录   select max(grade) from score;#查询score表中分数最高的记录   select count(*) from score;#查询score表中记录行数

(3)排序

  select * from score  order by stu_id desc ;#按照stu_id降序排序

(4)多表查询 

  多表查询是指从多张表中查询所需要的数据,一般查询的这几张表都有一个相同的字段关联这几张表。多表连接可以通过join关键字来连接,也可以直接用关联表中相同的id来进行关联;

  join:left join:左连接, 连接两张表,以左边表的数据匹配右边表中的数据,如果左边表中的数据在右边表中没有,会显示左边表中的数据。

  right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边表中的数据在左边边表中没有,会显示右边表中的数据。     

      inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会显示匹配的数据。

  select a.name 学生姓名,b.score 学生成绩 from students a left join score b on   a.id=b.student_id;     

      select a.name 学生姓名,b.score 学生成绩 from students a right join score b on   a.id=b.student_id;

  select a.name 学生姓名,b.score 学生成绩 from students a inner join score b on  a.id=b.student_id;  

    select a.name 学生姓名,b.score 学生成绩 from students a,score b wherea.id=b.student_id;

  连接查询:   内连接:select  表名.列名,……              from 表1 inner join 表2   on 条件     外连接:left join(左连接)   select  表名.列名,…表2.列名……   from 表1 left  (outer )join  表2    on 条件           right join(右连接)   select  表名.列名,…表2.列名……   from 表1 right  (outer )join  表2    on 条件  

(5)子查询  

  比如说要把成绩低于60分的学生的名字都改成笨蛋     

  update students set name = ‘笨蛋‘ where id in (select a.student_id from score a where a.score<60);

mysql常用操作

标签:

人气教程排行