吴振虎

导航

2,mysql的常用查询语句

一、数据库

1,创建数据库

create database database_name;

数据库的Name的命名一般按照公司规范,例如:

create database db_book;

2, 查看数据库

Show databases;

3,删除数据库

Drop database database_name;

例如:

Drop database db_book;

二、表结构的增删改

1.,创建一个表

Create table table_name(字段 属性,) CHARSET=utf8;

2,给表增加字段 add

Alter table table_name add filed_name 属性;

1,修改表名

Alter table old_tableName rename new_tableName;

2,给表修改字段 change

Alter table table_name  change  old_field_name new_field_name 属性;

3,删除表字段 drop

Alter table table_name drop field_name;

4,删除表

Drop table table_name;

 

三、单表查询

-- 查询所有的数据

select * from t_student ;

-- 查询年龄20的学生  where

select * from t_Student where age=21;

-- 查询年龄不等于20的学生 !=

select * from t_Student where age!=21;

-- 查询年龄20到21间的学生  between and

select * from t_Student where age between 20 and 21;

-- 初选年龄不在20到21间的学生

select * from t_Student where age not between 20 and 21;

-- 查询年龄在数组中的学生 in

select * from t_Student where age in (20, 21);

-- 查询 not in

select * from t_Student where age not in (20, 21);

-- 查询 null

select * from t_Student where sex is null;

-- 查询is not null;

select * from t_Student where sex is not null;

-- like 模糊查询_是一个词

select * from t_Student where stuName like '张_';

-- %是多个词

select * from t_Student where stuName like '张%';

-- 去除字段中的重复值

select distinct gradeName from t_Student;

-- GROUP BY必须分组相关数据,如果直接用*,会报错

select gradeName,GROUP_CONCAT(stuName)  from t_Student GROUP BY gradeName;

--having是对已经查出得结果,二次操作

SELECT sex,COUNT(stuName) FROM t_student GROUP BY sex HAVING COUNT(stuName)>2;

-- 从下标2开始,10个数据

select * from t_Student limit 2,10

四、 聚会函数查询

-- count,计数

select COUNT(*) as total from t_grade ;

-- 分组后再计数

 

-- 最大的得分,

select stuName,COUNT(*) as total from t_grade GROUP BY stuName;

当使用group by 时,前边必须查询分组字段

select stuName, MAx(score) from t_grade GROUP BY stuName;

-- avg平均数

select stuName, avg(score) from t_grade GROUP BY stuName;

五、 多表查询、内连接,左右外连接

-- 直接通过where实现多表的连接,内连接

select * FROM t_book tb, t_booktype tbk where tb.bookTypeId = tbk.id;

-- 通过table1 left join table2 on 表1属性=表2属性

select * FROM t_book tb LEFT JOIN t_booktype tbk on tb.bookTypeId= tbk.id;

-- 通过table1 right join table2 on 表1属性=表2属性

select * FROM t_book tb RIGHT JOIN t_booktype tbk on tb.bookTypeId = tbk.id;

-- 连接后,再次通过and来操作过滤

select * FROM t_book tb RIGHT JOIN t_booktype tbk on tb.bookTypeId = tbk.id and tb.price > 70;

-- 三张表连接

select * FROM (t_book tb RIGHT JOIN t_booktype2 tbk2 on tb.bookTypeId=tbk2.id ) RIGHT JOIN t_booktype tbk on tb.bookTypeId = tbk.id;

六、子查询

对已经查询的结果再次查询,in, any,all,exists

-- 对已经查询的结果再次查询,使用where in

SELECT * FROM t_book tb where tb.id in (SELECT id FROM t_booktype);

-- 对查询结果在查询,使用where exists 子查询有结果就查询外边

SELECT * FROM t_book tb where exists (SELECT id FROM t_booktype);

-- 对查询结果在查询,使用where not exists 子查询没有结果就查询外边

SELECT * FROM t_book tb where not exists (SELECT id FROM t_booktype);

-- 对查询结果在查询,使用where any 满足任何一个子查询的结果,就查询外边

SELECT * FROM t_book tb where price >= any (SELECT price FROM t_pricelevel);

-- 对查询结果在查询,使用where all 满足全部子查询的结果,就查询外边

SELECT * FROM t_book tb where price >= all (SELECT price FROM t_pricelevel);

七、联合查询

使用union,union all,和给表、字段取别名

-- union会自动去掉重复数据,union的两个表必须有相同的列数,按照第一个表的字段去排列数据

SELECT id FROM t_book union SELECT id FROM t_booktype ;

-- union all不去掉重复数据,union all的两个表必须有相同的列数,按照第一个表的字段去排列数据

SELECT id, price FROM t_book union all SELECT id,bookTypeName FROM t_booktype ;

-- 字段一般加上as,也可以省略,表后直接加别名,不需要as

SELECT tb.id as id1, tb.price as price2, tbk.id as tbk_id FROM t_book tb LEFT JOIN t_booktype tbk on tb.bookTypeId = tbk.id;

八、表数据的增删改

-- 增加表中数据行

insert into t_book (author) values('我爱我家'),('我爱我家2'),('我爱我家3');

-- 删除表中数据行

DELETE from t_book where author is Null;

-- 修改表中数据行

update t_book set author='' WHERE author like '我%' ;

 

posted on 2023-09-05 13:11  长生帝君  阅读(3)  评论(0编辑  收藏  举报