MySQL常用语句

发布于:2022-08-06 ⋅ 阅读:(330) ⋅ 点赞:(0)

一、基本语句

  1. 创建数据库
CREATE DATABASE databasename
  1. 删除数据库
drop database dbname
  1. 创建表
    新表:create table tabname(,,,)
    创建已有表:create table tab_new like tab_old
create table tab_new as select col1,col2… from tab_old definition only
  1. 删除表
    drop table tabname
  2. 增加一列
Alter table tabname add column col type
  1. 添加主键
Alter table tabname add primary key(col)
  1. 删除主键
Alter table tabname drop primary key(col)
  1. 创建索引
create [unique] index idxname on tabname(col….)
  1. 创建视图
create view viewname as select statement
  1. 增加
 insert into table1(field1,field2) values(value1,value2)
  1. 删除
delete from table1 where 范围
  1. 修改
update table1 set field1=value1 where 范围

13.查询

select * from table1 where field1 like%value1%

二、单表查询

2.1基本数据查询

  1. 查询所有字段数据
select * from 表名
  1. 查询部分数据
select name,gender,age from 表名
  1. 查询去重字段
select distinct age from 表名

2.2条件数据查询

  1. 查询指定记录
select * from 表名 where name = 'lihua'
  1. 范围查询
select name from 表名 where id in (001,002,003)
  1. 区间查询
 select name from 表名 where chese between 85 and 90
  1. 模糊查询
select name from 表名 where name like 'L%'
  1. 通配符查询
select name from 表名 where name '_L%'
  1. 非空查询
select name,chese from 表名 where chese is not null
  1. and多条件查询
select name,chese from 表名 where name like 'a%' and chese < 90
  1. or多条件查询(满足一个即可被查出来)
select name,chese from 表名 where name like 'a%' or chese < 90
  1. 对查询结果进行统计
 select name chese from 表名 order by chese ASC

ASC表示升序,DESC表示降序,默认升序,多字段排序 order by chese ASC,English DESC

2.3统计函数和分组查询

  1. count()记录条数(非空行)
    所有数据记录:select count(*) number from 表名
    指定字段记录:select count(name) number from 表名 where chese < 90
  2. avg()计算字段平均值
select avg(chese) avgerage from 表名
  1. sum()求和字段总值
select sum(chese) from 表名
  1. max(),min()字段最值
select max(chese),min(chese) from 表名
  1. group by分组查询
select * from 表名 group by subject
  1. having 限定分组查询
    在MySQL中不能同where语句对分组语句进行条件限制,需要having语句来实现
select name, age, avg(salary) average from 表名 group by age having avg(salary) >8000
  1. limit限定查询条数
 select * from 表名 where salary >8000 limit 3
select * from 表名 where salary >8000 limit 1,3  #(从第二条开始,限制3条)

三、多表查询

3.1内连接查询

  1. 自连接:表与其自身连接
select ts1.stuid,ts1.name,ts1.classno from t-student as ts1,t-student as ts2 where ts1.classno = ts2.classno and ts2.name = 'alica'
  1. join in 自连接
select ts1.stuid,ts1.name,ts1.classno from t-student as ts1  join in t-student as ts2 on ts1.classno = ts2.classno and ts2.name = 'alica'

3.2外连接查询

  1. 左外连接:左边全部显示,右边根据on语句筛选匹配
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
  1. 右外连接:右边全部显示,左边根据on语句筛选匹配
select a.a, a.b, a.c, b.c, b.d, b.f from a RIGHT OUT JOIN b ON a.a = b.c
  1. UNION并操作
select * from t1 union select * from t2

3.3子查询

  1. 子查询
    通俗的讲,子查询就是查询里还有查询语句
    select * from t1 where column1 = (select column1 from t2);
select * from t1 where id >= (select id from t2 where id = 3);
  • 子查询指嵌套在 查询内部,且必须始终出现在 圆括号内。
  • 子查询可以包含多个关键字或者条件,如DISTINCT,GROUP BY,ORDER BY,LIMIT,函数等
  • 子查询的外层查询可以是:SELECT,INSERT,UPDATE,SET,DO
  • 子查询的返回值可以是:标量、一行、一列,甚至是另一个子查询
  1. 带关键字的子查询

IN: 若条件想要落在子条件的范围里可以使用IN

select O_id,POL,POD from s_order where C_id in (select C_id from client where C_name like '佐%');

exits:子查询存在时才能进行外查询

select * from S_order where CNTR_Q > 2 AND exists (select c_ID from s_order where pod='RIGA');

any:any表示满足其他任意一个条件,>any表示大于任意一个值

select * from s_order where S_teu > any (select Tue_avg from Past_avg);

all:表示满足所有条件,>all 表示大于所有值

select * from s_order where S_teu > all (select Tue_avg from Past_avg);

网站公告

今日签到

点亮在社区的每一天
去签到