mysql_常用操作语句

个人博客


操作数据库

命令行进入数据库:mysql -u用户名 -p密码

显示数据库: show databases

新建数据库:create database databaseName

删除数据库:drop databse databaseName

选择数据库:use databaseName

查看所有数据表:show tables

查看表结构: desc tablesName


数据操作

增:insert into table (`field1`, `field2`...) values ('value1','value2'...)

删:delete from table where field1=value1

改:update tablename SET field1=value2 where field1=value1


查:

查询user表中所有数据:

select * from user;

where条件查询:

select * from user where id = 1002

as查询字段重命名:

select name as 名字 from user where id = 1002

排序

select * from user order by age, id asc

age和id字段正序排列(字段age优先级高)

select * from user order by id, age desc

age和id字段到序排列(字段id优先级高)


跳过限制查询数据:

select * from user where id>0 limit n

显示前面的n条数据

select * from user where id>0 limit m,n

跳过前面m条显示接下来的n条数据


模糊查询:

like + % 的使用(没有%, ‘like’ 等于 ‘=’)

name字段中包含‘ven’

select * from user where name LIKE "%ven%"

name字段中Lee开头:

select * from user where name LIKE "Lee%"

name字段中Jun结尾:

select * from user where name LIKE "%Jun"


范围查询

in语句:(查询idzai()内的数据)

select * from user where id in (1001,1002,1003)

between...and语句: (查询id范围1001到1010的数据)

select * from buser where id between 1001 and 1010


聚合:

统计记录条数:count(fidle)

select count(id) where id>1001

求和某个字段:sum(fidle)

select sum(age) where id>1001

求字段平均值:avg(fidle)

select avg(age) where id>1001

最大值:max(fidle)

select max(age) where id>1001

最小值:min(fidle)

select min(age) where id>1001


UNION 操作符:

用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据

select id,title from articles union select id, title from books order by id


分组查询结果:group by

对name字段分组并统计age字段的总条数

select name,count(age) from employee_tbl group by name

对name字段分组并统计age字段的平均值

select name,avg(age) from employee_tbl group by name

对name字段分组并统计age字段的最大值

select name,max(age) from employee_tbl group by name

对name字段分组并统计age字段的最小值

select name,min(age) from employee_tbl group by name

对name字段分组并统计age字段的和

select name,sum(age) from employee_tbl group by name

having查询条件

对name字段进行分组并统计singin的字段和条件为name<>'丽'

select name,sum(singin) from employee_tbl group by name HAVING name <> '丽'

注:

wherehaving

    where 与 having关键字都用于设置条件表达式对查询结果进行过滤,区别是having后面可以跟聚合

    函数,而where不能,通常having关键字都与group by 一起使用,表示对分组后的数据进行过滤


连接的使用:

内连接(INNER JOIN):获取两个表中字段匹配关系的记录

左连接(LEFT JOIN):获取左表所有记录,即使右表没有对应匹配的记录。

右连接(RIGHT JOIN):与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

数据表如下:

内连接的使用:

查询显示A表中的id,title和author与b表中的id字段,查询条件为a表中的ID字段=b表中的ID字段

select a.id,a.title,a.author, b.id from books a inner join articles b on a.id = b.id

左连接的使用:

查询左表(books)中的id,title和autho字段右表(articles)中的id和content字段,查询条件为左表中的ID字段=右表中的ID字段

select a.id,a.title,a.author, b.id,b.content from books a left join articles b on a.id = b.id

注:左连接会读取左边数据表的全部数据,即便右边表无对应数据,如下

右连接的使用:

查询左表()中的id,title字段,查询条件为左表中的ID字段=右表中的ID字段

select a.id, a.content,b.id,b.title,b.author from articles a right join books b on a.id = b.id

注:右连接则会读取右边数据表的全部数据,即便左边边表无对应数据;如下

发布了59 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43507959/article/details/96722755