MySql 语句大全 - 基础操作

目录

对数据库操作

1. 显示数据库

2. 使用数据库

3. 创建数据库

4. 删除数据库

对数据表操作

1. 创建新表:

2. 删除表:

3. 清空表:

4. 查看表结构:

5. 修改表: 

1) 修改表名:将表 tb_one 改名为 tb_two ;

2) 修改字段类型:

3) 修改字段名:

4) 增加列

5) 增加/删除主键

6) 增加/删除外键

对表数据操作(CRUD)

1. 查询:

1) 简单查询:

2) 条件查询:in

3) 模糊查询:

4) 排序:

5) 统计函数:

6) 分组查询:

7) 分页查询:

8) 去重查询:

2. 高级查询:

1)交叉连接(cross join):(不使用 ,毫无意义)

2)内连接(join .. on ):

3)外连接(left/right join):

4)联合查询:

5)子查询:

3. 修改:

4. 删除:

附录

模糊查询:Concat函数

用法: 字符串拼接 ;CONCAT(str1,str2,…) 

CONCAT_WS()函数

Group_concat函数

类型转换:Cast()函数


对数据库操作

1. 显示数据库

    show databases ;

2. 使用数据库

    use 数据库名称 ;

3. 创建数据库

create database 数据库名称 ;

# utf-8
create database 数据库名称 default charset utf8 collate utf8_general_ci;

# gbk
create database 数据库名称 default character set gbk collate gbk_chinese_ci;

4. 删除数据库

    drop database Database_Name

 

对数据表操作

1. 创建新表

# 使用旧表创建新表
create table 新表 like 旧表 ;
create table 新表 as select UserName1,UserName2… from 旧表 definition only
# 创建新表 1 (标准)

create table Table_Name (
id int(11) auto_increment primary key , //自增  主键

name varchar(255) default null,

…

) engine=InnoDB default charset = utf-8 ;

# 创建新表 2 (添加外键约束)

create table Table_Name (
id int(11) auto_increment primary key , //自增  主键

name varchar(255) default null,

… ,

constraint 外键索引【如: fk_two_one】foreign key (该表字段) references 父表(字段) 

//foreign key (该表字段) references 父表(字段)

)  ;

2. 删除表

    drop table 表名 ;

3. 清空表

    delete from 表名 ;

    truncate table 表名 ;

4. 查看表结构

    desc Table_Name;   //desc = describe

5. 修改表: 

1) 修改表名:将表 tb_one 改名为 tb_two ;

    alter table tb_one Rename tb_two  ;

2) 修改字段类型:

    alter table <表名> modify <字段名> <数据类型>  ;

3) 修改字段名:

    alter table <表名> change <旧字段名> <新字段名> <新数据类型> ;

4) 增加列

    alter table 表名 add column 新字段 数据类型

5) 增加/删除主键

    alter table 表名 add/drop primary key(字段)

6) 增加/删除外键

##例:alter table tb_two add constraint fk_two_one foreign key (oneid) references tb_one(id) ;

alter table 子表 add constraint 外键索引[如FK_子表_父表] foreign key (子表外键字段) references 父表(字段) ; //增加外键约束

alter table 子表 drop foreign key 外键索引  ; //删除外键约束

 

对表数据操作(CRUD

1. 查询

1) 简单查询:

select  *  from 表名 ;  # 查询所有字段

select id , name, … from 表名 ;

2) 条件查询:in

select * from 表名 where 字段2=值 一个条件查询
select * from 表名where 字段1='值 ' and 字段2=值 ;多条件 并关系 查询
select * from 表名where 字段1='值 ' or 字段2=值 ;多条件 或关系 查询
select * from 表名where 字段1>=50 and 字段<=60; 范围查询
select * from 表名where 字段between 50 and 60 ;范围查询

同个字段查询多个值:

select * from 表名where 字段 in (值1,值2,值3,...) ;

 

3) 模糊查询:

select * from 表名where Name like '%型'  //%通配符代表任意多个字符
select * from 表名 where Name like '%奥迪%'

select * from 表名where Name like '_马%'  // _通配符代表任意一个字符

4) 排序:

select * from 表名order by 字段asc ;  // 按照价格升序排列
select * from 表名order by 字段desc ;   // 按照价格降序排列
select * from 表名order by 字段1,字段2 ; //按照两列进行排序,前面的为主要的

5) 统计函数:

select count(*) from 表名 ;  // 查询表中有多少条数据
select max(Price) from 表名 ;   //取价格的最大值
select min(Price) from 表名 ;   //取价格的最小值
select sum(Price) from 表名 ;   // 取价格的总和
select avg(Price) from 表名 ;    //取价格的平均值

注: 使用函数,若带条件查询则不能使用where而应该使用HAVING

select sum(price) from table_user Having sum(price)>20 or id>2;

6) 分组查询:

select 字段from Car group by 字段having count(*)>2 ;  //查询所有系列中数量大于2的

7) 分页查询:

select * from 表名 limit 0,5 ;   // 从第条数据开始读取5条数据

8) 去重查询:

select distinct 字段from 表名;

 

2. 高级查询

1)交叉连接(cross join):(不使用 ,毫无意义

## 左表循环每条记录 直到右表所有记录 ,没有匹配条件:

select * from 左表 cross join 右表 ; // select * from 左表 , 右表 ;

2)内连接(join .. on ):

## 左表每条记录与右表的所有记录进行匹配, 匹配条件相同则保留:

select * from 左表 join 右表 on 条件 ;

select m.id,m.name,…, w.id,w.name,… from table_Left m join table_Right w on m.id = w.id ;

注:USING字段的使用,简写效果; 即两个表中的字段都为id ,则可以使用Using字段取代on字段。( USING(id) ==  m.id=w.id )

... join table_Right USING(id);

3)外连接(left/right join):

## 以某张表为主(所有记录保留),与另一张表进行连接,匹配条件保留,否则另一张表字段置为NULL;即无论是否符合On语句后面的条件,左/右主表中的数据都会被查询出来,另一张表未匹配的字段置为null

## left join ()连接 :以左表为主

## right join 右(外)连接 :以右表为主

select *from One_Table left join Two_Table on 条件 ;

select m.id,m.name,…, w.id,w.name,… from table_Left m left/right join table_Right w on m.id = w.id ;

4)联合查询:

## 联合查询结果不增加字段(类似将表2插入数据到表1中,但与数据类型无关);

## 联合查询意义:

1. 查询同一张表,但是需求不同: 如查询学生信息, 男生身高升序, 女生身高降序.

2. 多表查询: 多张表的结构是完全一样的,保存的数据(结构)也是一样的.

a)  Union/Union All

## 将多条select语句使用Union /Union All 拼接 ,但语句字段得相同 ;

## All: 保留所有数据 ; Distinct:去重(默认);

如:select 字段1,字段2, from 表1 Union /Union All select字段1,字段2, from 表1/2

b)  order by (limit

## 使用时需对 select语句使用括号,且 必须使用limit:限定的最大数量;

(select … from 表1 where 条件 order by 字段 limit 99999) union (select … from 表2 where 条件 order by 字段 desc limit 99999)   // 正逆序排列

5)子查询:

##子查询有两种分类方式: 按位置分类;和按结果分类

  • 按位置分类: 子查询(select语句)在外部查询(select语句)中出现的位置
  1. From子查询: 子查询跟在from之后
  2. Where子查询: 子查询出现where条件中
  3. Exists子查询: 子查询出现在exists里面
  • 按结果分类: 根据子查询得到的数据进行分类(理论上讲任何一个查询得到的结果都可以理解为二维表)
  1. 标量子查询: 子查询得到的结果是一行一列
  2. 列子查询: 子查询得到的结果是一列多行
  3. 行子查询: 子查询得到的结果是多列一行(多行多列) (1,2,3出现的位置都是在where之后)
  4. 表子查询: 子查询得到的结果是多行多列(出现的位置是在from之后)

 

例如:标量子查询

select 字段1 from table 1 where 条件(id)= (select 字段1(外键) from table_2 where 条件2 = 值)

3. 修改

update 表名 set 字段 = 新值 where 字段 = 值 ;

4. 删除

delete from 表名 where 字段 = 值 ;


附录

模糊查询:Concat函数

用法: 字符串拼接 ;CONCAT(str1,str2,…) 

当Concat中某个值为NULL时,返回NULL;

m.table_Name like concat(concat('%',#{tableName}),'%')----------------------就是拼接成% 变量值%

select * from m where m.name like concat(concat('%','#tableName'),'%');

 

CONCAT_WS()函数

用法:concat_ws(separator,str,str2,...); 字面意思:concat with separator,合并和分隔符

即:第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。

mysql> SELECT CONCAT_WS(’,',’First name’,'Second name’,'Last Name’);
-> ‘First name,Second name,Last Name’

 

Group_concat函数

 

类型转换:Cast()函数

用法:Cast(字段名 as 转换的类型)。其中类型为:

CHAR[(N)] 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int
TIME 时间型

 

 

猜你喜欢

转载自blog.csdn.net/StarryaSky/article/details/82791773
今日推荐