最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

Mysql数据库的常用操作_mysql数据库的查询操作

XAMPP相关 admin 1201浏览 0评论
本文主要介绍mysql数据库的查询操作,捎带脚增删改操作

·增 ·

insert into:

 

insert into table (字段) values (数据)(数据)…

 

insert into table values (数据)(数据)…

 

insert into table (列名,列名…) select (列名,列名…) from 表

·删 ·

drop:

 

◆ drop datebase “数据库名” — 删除数据库

 

◆ drop table “表名” — 删除表

 

delete from:

 

◆ delete from table — 删除表中数据

 

◆ delete from table where条件 — 按条件删除表中数据

·改 ·

update:

 

update “表名” set 字段=value where 条件

· 查 ·

select:

 

单表查询:

 

◆ select * from table where 条件

 

◆ select id as c_id from table where 条件

 

多表关联查询:

 

◆ 内联查询

 

 

select * from table1,table2… where 关联条件 and 查询条件

 

select * from table1 inner join table2 on 关联条件 … where 查询条件

 

◆ 左联查询

 

select * from table1 left join table2 on 关联条件 … where 查询条件

 

◆ 右联查询

 

效果与左联查询效果相反

 

select * from table1 right join table2 on 关联条件 … where 查询条件

 

◆ union查询

 

左联查询、右联查询的查询结果去重(去掉重复数据)后进行合并

 

select * from table1 left join table2 on 关联条件

 

union (union all 为不去重将结果全部合并)

 

select * from table1 right join table2 on 关联条件

 

◆ 高级查询

 

order by—对查询结果进行排序

 

select * from table where 条件 order by 字段(需要排序的字段) asc\desc

 

select * from table order by 列1 asc,列2 desc…

 

(根据多列进行排序,若列1数据相同,根据列2进行排序)

 

in—条件在数据集\子查询中

 

select * from table where 条件字段 in 数据集\子查询

 

like—根据条件模糊查询

 

select * from table where 条件字段 like ‘%_值’ (%–任意字符、_–每个下划线代表一个字符)

 

group by—根据某字段进行分组查询

 

select * from table where 条件 group by having

 

between and—查询条件在某个区间之间

 

select * from table where 条件字段 between … and …

 

distinct—对查询结果进行去重

 

select distinct(去重字段) from table where 条件

 

limit—分页

 

select * from table where 条件 limit m,n

 

select * from table where 条件 limit n offset m

 

(从偏移m个数据,查询n条)

 

◆ 数据库查询常用函数

 

数值相关函数:

 

max()—最大值、min()—最小值

 

avg()—平均值、sum()—求和

 

count() — 计数、median() — 中位数

 

日期相关函数:

 

sysdate()—当前日期时期

 

curdate()—当前日期

 

curtime()—当前时间

 

year()—获取日期的年份

 

month()—获取日期的月份

 

date_add(日期,interval 变更值 单位)—变更日期

 

字符串相关函数:

 

concat()—拼接字符串

 

length()—获取字符串长度

 

substr()—截取字符串

其他 

◆ desc “table”

 

列出表的信息(包含表结构、字段、字段类型、主键、外键等)

 

◆ use “datebase”

 

打开数据库

 

◆ <>

 

条件中的不等于

 

sql执行顺序

 

◆ select … from … where … group by … having … order by …

 

当一条sql语句同时存在where、group by、having、order by关键字时,执行顺序:

 

(1) FROM left_table

 

(2) ON join_condition

 

(3) join_type JOIN right_table

 

(4) WHERE where_condition

 

(5) GROUP BY group_by_list

 

(6) HAVING having_condition

 

(7) SELECT

 

(8) DISTINCT select_list

 

(9) ORDER BY order_by_condition

 

(10) LIMIT limit_number

转载请注明:XAMPP中文组官网 » Mysql数据库的常用操作_mysql数据库的查询操作

您必须 登录 才能发表评论!