lv_mysql_mysql数据库

一、数据库

查询所有数据库

show databases;

查询单个数据库:

show create database emp;

修改数据库:

alter database emp1 character set utf8;

修改emp1的字符集为utf8;

删除数据库:

drop database emp1;

二、表操作

show tables;

查询所有表;

创建表:

使用之前先use mydb01;

create table emp(

id int;

name varchar(20),

age int;

time data;

price double 浮点型不需要加‘’

);

查询表结构:

desc emp1;

+-------+-------------+------+-----+---------+-------+

| Field | Type        | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| sid   | int(11)     | YES  |     | NULL    |       |

| sname | varchar(20) | YES  |     | NULL    |       |

| sage  | int(11)     | YES  |     | NULL    |       |

+-------+-------------+------+-----+---------+-------+

删除表:

drop table emp1;

修改表:

1、添加字段

alter table emp1 add column age int;

在emp1表中添加age值为int的一列;

2、修改字段类型

alter table emp1 modify column age  int;

修改age的类型为int;

3、修改字段名称

alter table emp1 change age column age1 varchar(10);

修改字段名称为age1

4、删除字段

alter table emp1 drop column age1;

三、增删改数据

1、增加

8c1e56bc01fb72f20141b2aa9e3290e3.png

insert intostudent values(5,'帅气','2017-08-09','女');

插入整行

INSERT INTO student (pid,aname) VALUES (2,'帅气');

插入部分pid和aname值

二、修改

UPDATE student SET qdate='1997-05-14',aname='帅气的我' WHERE pid=2;

修改第二行qdate和aname的值。

三、删除

delete from student where pid=2;

删除第二行

delete from student;

删除student表

TRUNCATE TABLE student;

清空表,并且清空约束

四、查询

select * from student;

四、查询数据 12种

1、查询所有的行

select * from student;

查询指定的列

select  pid,aneme * from student;

查询pid和aname列

2、查询的时候添加常量

select pid as ‘梦想’,aname as ‘种种’ *from student;

查询时将pid变为梦想,aname变为种种

3、查询时合并量

SELECT aname,(html+js+jquery) AS 'grade' FROM score;

d22f4df74c25c5013ba69d697f2363b6.png

4、查询时去掉重复列

select distinct sex from student;

c320309ca9ab440ae6833e0bdd243592.png

5、条件查询

逻辑条件与或

and与,左右全部满足;or或左右只满足一个即可

select * from student where sex=‘女’ and aname=‘帅气的我’;

782f72c44e10f43a3ec830478ddaf758.png

因为需要满足两边,所以选不到名称为男的帅气的我

非:or

select * from student where sex=‘女’ or aname=‘美丽’;

273d55cef08fe067609ffb9449890ac5.png

因为只满足其一,所以美丽为男也被入选

判断条件

><<>>=<= between and等同>=,<=

select * from score where js<>20;

339086636adb63377456e3f954a0e257.png

select * from score where jq between 30 and 80;

select * from score where jq >=30 and jq<=80;

SELECT * FROM score WHERE html<60;

74492375c9c1fc4f33f1251334105504.png

判断为空:

select * from score where sex is null;

不为空

SELECT * FROM student WHERE sex IS NOT NULL;

f6c9bcbcdf37743a4e71b9e9a634be55.png

查询性别为空的字符串

select * from student where sex=‘’;

判断性别不为空字符串的数据

select * from student where sex <>'’ and sex is not null;

首先判断sex不为空值,且不为空字符串

模糊查询

% 任意个

_一个

select * from student where aname like'张%';

姓张的同学

select * from student where aname like‘张_’;

姓张并两位名

SELECT *FROM student WHERE aname LIKE('%丽%');

SELECT *FROM student WHERE aname LIKE('梦%丽%');

ceb9ad18e27fd23918d68313d19e4cfb.png

-- 求js的总成绩 html+js+jq

SELECT SUM(js+jquery+html) FROM score;

-- 求js的平均成绩

SELECT AVG(js) FROM score;

-- 求html的最大成绩

SELECT MAX(html) FROM score;

-- 求html中的最小成绩

SELECT MIN(html) FROM score;

-- 查询本班有多少学生

SELECT COUNT(pid) FROM student;

-- 分页查询 x,x 起始行=(当前页-1),每页显示的条数

SELECT * FROM student LIMIT 0,2;

SELECT * FROM student LIMIT 2,2;

SELECT * FROM student LIMIT 4,2;

-- js成绩从大到小排序 ↓

SELECT * FROM score ORDER BY js DESC;

-- jq成绩从小到大排序 ↑

SELECT * FROM score ORDER BY jquery ASC;

-- 按照js的成绩从小到大,jq成绩从大到小,谁前面谁为主,如果js成绩一样,则jq按照jq为主。

SELECT *FROM score ORDER BY js ASC ,jquery DESC;

分组查询

-- 按照性别分组

SELECT sex FROM student GROUP BY sex;

-- 按照分组来统计人数,和聚合函数count(*)一起

SELECT sex,COUNT(*) FROM student GROUP BY sex;

SELECT class, COUNT(*) FROM score GROUP BY class;

-- 统计每个班的js总分

SELECT class,SUM(js) FROM score GROUP BY class;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值