Mysql基础语句(持续更新)

2019-06-26  本文已影响0人  今天学习吗

 1、创建一张表

命令:

create table 表名(

列名1 数据类型(长度) primary key,

列名2 数据类型(长度) not null

);

2、查看表的详细信息

命令:desc 表名

//desc test_name

3、重命名表名

命令:alter table 旧表名 rename 新表名

//alter table test_name rename tester_name

4、添加列

命令:alter table 表名 add 新列名 类型(长度) not null;

5、修改列

命令:alter table 表名 change 就列名 新列名 新类型(新长度) null;

6、删除列

命令:alter table 表名 drop 列名;

//alter table tesrer_name drop name

7、插入表数据

命令:insert into 表名 (列名1,列名2,列名3......) values (值1,值2,值3......);

//insert into tester_name(name,age)values('xixi',24)

8、修改表记录

命令:update 表名 set 修改内容 where 条件 // where 后面加条件

//update tester_name set  password=123456 where id=1 

9、删除表记录

命令:delete from 表名

//delete from tester_name

10、比较运算符在sql语句中的使用

//select * from students where english>90;

11、select 语句的使用

命令:select *from 表名 where 条件

12、模糊查询

% 匹配所有的字符 __匹配单个字符

//select *from ecs_goods where goods_name like “%邹%”

13、in 在条件语句中的使用 (in 表示在什么里面)

命令:SELECT column_name(s)  FROM table_name  WHERE column_name IN (value1,value2,...)

//select *from ecs_goods where goods_sn in("ECS000139","ECS000140","ECS000141");

14、嵌套语句

举例:select * from ecs_goods where goods_id in (select goods_id from ecs_order_goods); //查询出订单的产品的所有信息

Select *from ecs_users where user_id in (select user_id from ecs_order_info);   //查询出是那些人下的订单

注意:in中的sql语句可以当成条件处理,in括号里面的sql语句只能为一列数据

15、Between and 在条件中使用

操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期

命令:SELECT column_name(s)  FROM 表名  WHERE column_name  BETWEEN value1 AND value2

举例:select *from ecs_goods where shop_price between 4000 and 8000   //查询商品价格在4000到8000之间,有包括4000和8000

注意:使用between and的时候,小的值放前面,大的值放后面

16、把列名自定义为中文或者其他名字

举例:select goods_id as “商品id”,goods_name as “商品名称”,(market_price+shop_price) as “价格总和” from ecs_goods where (market_price+shop_price)>15000   //查询出market_price+shop_price之和大于15000并且展示之和

select goods_id as "商品id",goods_name as "商品名称",(market_price-shop_price) as "价格差"  from   ecs_goods where (market_price-shop_price) > 1000;         //查询2个价格差价大于1000

Select goods_id as "商品id",goods_name as "商品名称",(goods_number*shop_price) as "预估销售额"  from   ecs_goods; //求商品预估销售额

17、排序

命令:Order by 列名 asc   //从小到大排序

          Order by 列名 desc  // 从大到小排序

          Order by 列名      //默认不写,从小到大排序

          select *from 表名 order by 列名

//Select *from ecs_goods order by shop_price asc; (升序)

//Select *from ecd_goods order by shop_peice desc;(降序)

18、count统计函数

命令:SELECT COUNT(*) FROM 表名  //COUNT(*)     //函数返回表中的记录数

           SELECT COUNT(column_name) FROM 表名    //COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入)

            SELECT COUNT(DISTINCT column_name) FROM 表名     //COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目

//select  count(*) from ecs_goods;  // 可以统计条数

//select  count(goods_id) from ecd_goods;  // 可以统计记录条数,但是如果列中包含null,就不能统计null

19、max最大值函数/min最小值函数/平均值函数

命令:select max(列名)from 表名; 最大值

//Select min(shop+peice)  from ecs_goods; 最小值

//Select avg(shop_price) from ecs_goods;  平均值

20、sum求和函数

命令:SELECT SUM(column_name) FROM 表名

举例:select  sum(ckick_count) from ecs_goods;

21、distinct 去掉重复数据函数

命令:SELECT DISTINCT 列名称 FROM 表名称

//select distinst(tname)from teacher;   # 对应的列去掉重复数据

22、group by 分组

合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句。

//SELECT gender,sum(gender) AS 姓名统计 from patient GROUP BY gender ORDER BY sum(gender) DESC;

select 类别, sum(数量) as 数量之和   from A    group by 类别

23、where 跟 having 的区分?

(1)没有分组之前可以用where,也可以用having 。

(2)分组之后,只能用having,不能用where

24、左关联

LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行

命令:SELECT column_name(s)  FROM table_name1  LEFT JOIN table_name2  ON table_name1.column_name=table_name2.column_name

25、右关联

RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行

命令:SELECT column_name(s)  FROM table_name1  RIGHT JOIN table_name2  ON table_name1.column_name=table_name2.column_name

26、内关联

在表中存在至少一个匹配时,INNER JOIN 关键字返回行

命令:SELECT column_name(s)  FROM table_name1  INNER JOIN table_name2  ON table_name1.column_name=table_name2.column_name

#多表关联查询:

select 

patient.name,patient.age,patient.gender,patient.phone,patient.work_state,patient.quarantine_area,patient.role,patient.job_type,

topic_exam.score1,topic_exam.level_name1,topic_exam.score2,topic_exam.level_name2,topic_exam.score3,topic_exam.level_name3,topic_exam.create_time

FROM patient,topic_exam WHERE patient.id=topic_exam.user_id

#求学生成绩总和

select student_id,sum(number) from score where student_id>=2  group by student_id having sum(number)>170;

#求每个学生的平均成绩

select student_id,avg(number) from score GROUP BY student_id having avg(number)>80;

注意:分组之前运行的条件可以放在where 后面, 分组之后的条件只能放在group by后面。

#列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

select sno from sc where scgrade<60 group by sno having count(cno)>=2

#查询所有学生所有的测评记录

select t.score1,t.level_name1,t.score2,t.level_name2,t.score3,t.level_name3,t.create_time,p.name,p.job_type,p.phone,p.age

from patient p,topic_exam t  where p.id=t.user_id  and p.job_type=5

上一篇 下一篇

猜你喜欢

热点阅读