日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mysql基础2

發布時間:2023/12/9 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql基础2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


清空一個表,自增id從1開始
truncate table 表名;

查詢
select 列名 from 表名 where 條件 order by 列名 [desc|asc] limit 跳過條數,查多少條

AS 別名
列名 as 新列名 注意as可以省掉不寫
NULL值查詢
select * from table1 where 字段 is null;
組合列
select concat('No.',id) from stu;
去重復
select distinct 列名 from 表名; 注意:列名,只能跟一個
排序
select * from 表名 order by 列名1 asc,列名2 desc;
加工之后的信息排序
select concat('No.',id) from stu order by concat('No.',id);
常用函數
select concat('My','S','QL'); 連接
select lower('PHP'); 轉小寫
upper() 轉大寫
select replace('www.mysql.com','www','http://www'); 替換
update stu set name = replace( replace(name,'o','0') ,'i' ,'1'); 將name字段所有的o換為0,所有的i換為1

trim()
ltrim()
rtrim()
select abs(-5) 絕對值
select adddate('2013-02-28',1); 2013-03-01
select now();

通配符 "%" 任意 "_"單個
select * from stu where name like '張_';

正則 不支持 \w ...
select * from stu where name regexp '^a[a-z][a-z]$';

區間操作 [not] between ... and ...
select * from stu where age >=18 and age <=19;
select * from stu where age between 18 and 19;
select * from stu where age not between 18 and 19;

[not] in (值1,值2,...)

sum 求和
avg 平均
count(*) 計數
括號中可以是*,也可以列名
如果括號中是列名,則不會統計該列數據為null值的
max 最大值
select max(age) from stu;
min 最小值

*分組查詢
一條語句得到男生和女生分別是多少人?
select sex,count(*) from stu group by sex; #分組后統計
注意:字段只能是用于分組的字段(group by 后的字段) 或聚合函數

按科目分組統計平均成績

分組帶排序
select sex,avg(age) as avg from stu group by sex order by avg desc;

*分組后篩選 having
按性別分組,求平均年齡大于等于6
select sex,avg(age) as avg from stu group by sex having avg>=6;
按性別分組,求平均年齡大于等于6,按平均年齡從大到小排列
select sex,avg(age) as avg from stu group by sex having avg>=6 order by avg desc;

where 與having可以同時出現,where在篩選之前就執行了,having是在分組之后才執行

顯示人數超過15人的班級,按班級人數排序
select grade_id,count(*) from stu group by grade_id having count(*)>15 order by count(*) desc;
select grade_id,count(*) total from stu group by grade_id having total>15 order by total desc;

WHERE -> GROUP BY -> HAVING ->ORDER BY


成績表
create table score (
id int auto_increment primary key,
stu_id int,
subject varchar(50) ,
score int
) engine=myisam default charset=utf8;

INSERT INTO score (stu_id,subject,score) values (1,'php',65),(1,'mysql',95),(1,'linux',80),(2,'php',50),(2,'mysql',70),(2,'linux',70);

統計及格總人數和及格人數的平均分
select subject, count(*),avg(score) from score where score>=70 group bysubject;
統計及格總人數和平均分在80以上


多個學員不低于90分的班級
select 班級 from 表名 where score >=90 group by 班級 having count(*)>1


顯示:id,姓名,班級名稱
select stu.id,stu.name,grade.name from stu,grade where stu.grade_id=grade.id;
顯示:id,姓名,班級名稱,age大于10的
select stu.id,stu.name,grade.name,age from stu,grade where stu.grade_id=grade.id and age>10;

select stu.id,stu.name,grade.name,age from stu
left join grade on grade.id=stu.grade_id
where age>10;

左聯接
左表中的數據,全部出來,就算右表沒,沒有對應的數據
select stu.id,stu.name,grade.name,age from stu
left join grade on grade.id=stu.grade_id

右聯接
select stu.id,stu.name,grade.name,age from stu
right join grade on grade.id=stu.grade_id

用右連接,實現和前面一樣的效果
select stu.id,stu.name,grade.name,age from grade
right join stu on grade.id=stu.grade_id

內聯 inner可以省掉
select stu.id,stu.name,grade.name,age from stu
inner join grade on grade.id=stu.grade_id
內聯與這樣結果相同: select * 表1,表2 where 條件


顯示所有參加考試的男生的成績
select score.*,stu.name from stu join score on score.stu_id=stu.id where sex=1;
select score.*,stu.name from stu ,score where stu.id=score.stu_id and sex=1;
先查男生的id,然后通過id列表,去成績表中查詢
select id from stu where sex=1; select * from score where stu_id in (1,6,8);
select * from score where stu_id in (select id from stu where sex=1);


查詢科目及格人數和不及格人數:
科目 及格人數 不及格人數
phy 20 1
mysql 26 2

select subject,sum(case when score<60 then 1 else 0 end) as no,
sum(case when score>=60 then 1 else 0 end) pass
from score group by subject;


轉載于:https://www.cnblogs.com/wicub/p/4322933.html

總結

以上是生活随笔為你收集整理的Mysql基础2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。