Mysql 用法

时间:2023-03-08 22:40:27

一转眼,一个星期过去了,来到测试班也一个星期了,今天经历了一次,这是自己这一周的总结,也算对自己这一周的一个交代。

几个比较重要的语句:

查看数据库

show databases;

创建数据库

create database student;

删除数据库

drop database student;

进入数据库

use student;

查看表

show tables;

创建表"   字段 数据类型 约束条件, 主键。一个表由,字段,数据类型,约束条件组成。

例如 create table stu(

sno  int(11) primary key,

name varchar(20),

sex  varchar(10) default "

age  int(11) default 10

删除表

drop table stu;

查看表结构

desc stu;

数据的增删改查

查询 selcet * from 表名

条件查询

Where后面跟条件  条件要写清楚

例如,查询成绩表中成绩(degree)为92的信息

Select * from score where degree =”92”;

查询成绩表中课程号是3-245并且成绩是86的信息

Select * from score where cno='3-245' and degree=”86”或者用or  并且用and

select * from teacher where tname = "李诚" and tsex = "男"; 查找老师名字叫李诚并且性别是男的

查 哪些列 从 哪个表 条件是 姓名为张三

select * from student where name= “张三”;

模糊查询  like    not like  后加%

查找老师表中姓李的 名字是两个字老师

select * from teacher where tName like '%李%'

%代表任意多个字符  _代表一个字符

排序查询 order by 字段 排序值(desc(降序)/asc(升序)默认的是升序 )

select * from student order by class asc

范围查询 关系运算符  between。。。and  在.... 之间

select * from Car where Price>=40 and Price<=60   这个小汽车的价格大于等于40小于等于60

select * from Car where Price between 40 and 50  这个小汽车的价格在40和50之间

离散查询 in   not in

select * from student where sname in ('张三','李四') 从学生里面查找名字叫张三,李四的

Seclect * from student where sname =“张三” or  sname =“李四”

重查询  distinct  不同的

select distinct cno from score

聚合函数,统计查询

select sum(字段名) from  表名  查询所有价格之和 sum()求和

select count(Code) from Car #查询数据条数

select max(Code) from Car #求最大值

select min(Brand) from Car #求最小值

select avg(Price) from Car #求平均值

外层查询 (里层查询)

子查询的结果当做父查询的条件

子查询:select Code from Nation where Name='汉族'

父查询:select * from Info where Nation = ''

select * from Info where Nation = (select Code from Nation where Name='汉族

分页查询 limit 从第几条开始,取多少条数据

#每页显示5条数据,取第5页的数据

select * from student limit (pageSize-1)*5,5

分组查询   group by 字段  having 条件

select

count(*),cno,group_concat(degree),sum(degree)

from score group by cno ;

select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3

#分组之后根据条件查询使用having 不使用where