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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql命令查询语句

發(fā)布時(shí)間:2024/9/5 数据库 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql命令查询语句 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

?

1、單表查詢

select * from student; 采用*效率低,不推薦,多用列名 一、單表查詢的語(yǔ)法:SELECT 字段1,字段2... FROM 表名WHERE 條件GROUP BY fieldHAVING 篩選ORDER BY fieldLIMIT 限制條數(shù)二、關(guān)鍵字的執(zhí)行優(yōu)先級(jí): fromwheregroup byhavingselectdistinct 去重處理order bylimit

補(bǔ)充說(shuō)明:

#查詢使用別名:

#查詢過(guò)濾重復(fù)

#連接查詢

2、多表查詢

交叉連接:不適用任何匹配條件。生成笛卡爾積 內(nèi)連接:只連接匹配的行 外鏈接之左連接:優(yōu)先顯示左表全部記錄 外鏈接之右連接:優(yōu)先顯示右表全部記錄 全外連接:顯示左右兩個(gè)表全部記錄

# 分頁(yè)limit

# 聚合函數(shù)

sum返回一列的總和

#MySQL教程之concat以及group_concat的用法

一、concat()函數(shù)1、功能:將多個(gè)字符串連接成一個(gè)字符串。2、語(yǔ)法:concat(str1, str2,...)返回結(jié)果為連接參數(shù)產(chǎn)生的字符串,如果有任何一個(gè)參數(shù)為null,則返回值為null。 select concat (id, name, score) as info from tt2;

?

?

group_concat() 1、功能:將group by產(chǎn)生的同一個(gè)分組中的值連接起來(lái),返回一個(gè)字符串結(jié)果。 2、語(yǔ)法:group_concat( [distinct] 要連接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] ) 說(shuō)明:通過(guò)使用distinct可以排除重復(fù)值;如果希望對(duì)結(jié)果中的值進(jìn)行排序,可以使用order by子句;separator是一個(gè)字符串值,缺省為一個(gè)逗號(hào)。3、舉例:例7:使用group_concat()和group by顯示相同名字的人的id號(hào):

?#合并

?#注意 union與union all的區(qū)別:union會(huì)去掉相同的紀(jì)錄

?

# 通配符

#exists

EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時(shí),內(nèi)層查詢語(yǔ)句不返回查詢的記錄。 而是返回一個(gè)真假值。True或False 當(dāng)返回True時(shí),外層查詢語(yǔ)句將進(jìn)行查詢;當(dāng)返回值為False時(shí),外層查詢語(yǔ)句不進(jìn)行查詢
select * from employee-> where exists-> (select id from department where id=200);

1、select 字段 from 表名 查詢條件
2、limit
3、select 字段 from 左表名 inner/left/right join 右表名 on 條件

?mysql練習(xí)題

聯(lián)合唯一,比如同一個(gè)學(xué)生不能選重復(fù)的課程
unique(student_id,course_id),

unique與primary key的區(qū)別:

簡(jiǎn)單的講,primary key=unique+not null具體的區(qū)別:(1) 唯一性約束所在的列允許空值,但是主鍵約束所在的列不允許空值。(2) 可以把唯一性約束放在一個(gè)或者多個(gè)列上,這些列或列的組合必須有唯一的。但是,唯一性約束所在的列并不是表的主鍵列。(3) 唯一性約束強(qiáng)制在指定的列上創(chuàng)建一個(gè)唯一性索引。在默認(rèn)情況下,創(chuàng)建唯一性的非聚簇索引,但是,也可以指定所創(chuàng)建的索引是聚簇索引。(4) 建立主鍵的目的是讓外鍵來(lái)引用.(5) 一個(gè)表最多只有一個(gè)主鍵,但可以有很多唯一鍵

聯(lián)合主鍵和復(fù)合主鍵區(qū)別

create table test(id int(10) not null auto_increment,name varchar(20) not null,sex int(1) not null,primary key (id,name,sex) );

?

1、學(xué)生表:student(學(xué)號(hào),學(xué)生姓名,出生年月,性別)

create table student(id int,name char(6),born_year year,birth_date date,class_time time,reg_time datetime );insert into student values (1,'egon',now(),now(),now(),now());insert into student values (2,'alex',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"); 日期類型

?

create table student(學(xué)號(hào) int primary key ,學(xué)生姓名 char,出生年月 date,性別 enum('男',''))

2、成績(jī)表:score(學(xué)號(hào),課程號(hào),成績(jī))

錯(cuò)誤寫法:
create table score(學(xué)號(hào) int primary key ,課程號(hào) int,成績(jī) float,unique(學(xué)號(hào),課程號(hào)))

這樣設(shè)置表就沒(méi)有主鍵了 正確寫法: 聯(lián)合主鍵:create table score(學(xué)號(hào) int,課程號(hào) int,成績(jī) float,primary key(學(xué)號(hào),課程號(hào)));

3、課程表:course(課程號(hào),課程名稱,教師號(hào))

create table course(課程號(hào) int primary key,課程名稱 char,教師號(hào) int)

4、教師表:teacher(教師號(hào),教師姓名)

create table teacher(教師號(hào) int primary key,教師姓名 char)

?插入數(shù)據(jù):在插入數(shù)據(jù)前用navicat或者sql語(yǔ)句檢查一下各字段的字符長(zhǎng)度

desc student;

?

(1)向?qū)W生表中

insert into student(學(xué)號(hào),學(xué)生姓名,出生年月,性別) values(1,'猴子','1989-01-01','男'), (2 , '猴子' , '1990-12-21' , '女'),
(3 , '馬云' , '1991-12-21' , '男'),
(4, '王思聰' , '1990-05-20' , '男');

(2)成績(jī)表

insert into score(學(xué)號(hào),課程號(hào),成績(jī)) values(1,1,80),
(1,2,90),
(1,3,99),
(2,2,60),
(2,3,80),
(3,1,80),
(3,3,80);

(3)課程表

insert into course(課程號(hào),課程名稱,教師號(hào)) values(1,'語(yǔ)文',2), (2,'數(shù)學(xué)',1),(3,'英語(yǔ)',3);

(4)教師表

insert into teacher(教師號(hào),教師姓名) values(1,'孟扎扎'), (2,'馬化騰'),(3,null),(4,'');

查詢語(yǔ)句

1、查詢姓‘猴’的學(xué)生名單
select 學(xué)生姓名 from student where 學(xué)生姓名 like '猴%';
2、查詢姓名中最后一個(gè)字是‘猴’的學(xué)生名單
select 學(xué)生姓名 from student where 學(xué)生姓名 like '%猴'; 3、查詢姓名中帶‘猴’的學(xué)生名單 select 學(xué)生姓名 from student where 學(xué)生姓名 like '%猴%';

‘猴%’匹配以猴字開(kāi)頭的? ?猴 后面有沒(méi)有字符無(wú)所謂? % 任意多個(gè)字符

‘猴_’匹配 以猴字開(kāi)頭 兩個(gè)字符? ? ? ? ? ? ? ? ?_ 任意一個(gè)字符


匯總分析:
1、查詢課程編號(hào)為2的總成績(jī) select sum(成績(jī)) as 課程編號(hào)為2總成績(jī) from score where 課程編號(hào)=2;
2、查詢選了課程的學(xué)生人數(shù)? select count(distinct 學(xué)號(hào)) as 選課人數(shù) from score;

?分組:

1、查詢各科成績(jī)的最高分和最低分
select 課程號(hào),max(成績(jī)),min(成績(jī)) from score group by 課程號(hào);
2、查詢每門課程被選修的學(xué)生數(shù)
select 課程號(hào),count(學(xué)號(hào)) from score group by 課程號(hào);
3、查詢男生、女生人數(shù)
sum是求和,count是計(jì)數(shù)
select 性別, count(*) from student GROUP BY 性別;

分組結(jié)果的條件

1、查詢平均成績(jī)大于60分學(xué)生的學(xué)號(hào)和平均成績(jī)
select 學(xué)號(hào) ,avg(成績(jī)) from score group by 學(xué)號(hào) having avg(成績(jī))>60 ;
2、查詢至少選修兩門課程的學(xué)生學(xué)號(hào)
select 學(xué)號(hào) from score group by 學(xué)號(hào) having count(學(xué)號(hào))>=2;
3、查詢同名同性學(xué)生名單并統(tǒng)計(jì)同名人數(shù)
select 學(xué)生姓名 ,count(*) as 人數(shù) from student group by 姓名 having count(*)>1;
相同
select 學(xué)生姓名,count(學(xué)生姓名) from student group by 學(xué)生姓名 having count(學(xué)生姓名)>1;

?

4、查詢不及格的課程并按課程號(hào)從大到小排列
select 課程號(hào),成績(jī) from score where 成績(jī) <60 order by 課程號(hào) desc;
5、查詢每門課程的平均成績(jī),結(jié)果按平均成績(jī)升序排序,平均成績(jī)相同時(shí),按課程號(hào)降序排列
select 課程號(hào),avg(成績(jī)) as 平均成績(jī) from score group by 課程號(hào) order by 平均成績(jī) asc,課程號(hào) desc;
6、檢索課程編號(hào)為“0004”且分?jǐn)?shù)小于60的學(xué)生學(xué)號(hào),結(jié)果按按分?jǐn)?shù)降序排列
select 學(xué)號(hào) from score where 課程號(hào)=4 and 成績(jī)<60 order by 成績(jī) desc;
7、統(tǒng)計(jì)每門課程的學(xué)生選修人數(shù)(超過(guò)2人的課程才統(tǒng)計(jì))要求輸出課程號(hào)和選修人數(shù),查詢結(jié)果按人數(shù)降序排序,若人數(shù)相同,按課程號(hào)升序排序

select 課程號(hào) ,count(學(xué)號(hào)) as 選修人數(shù) from score group by 課程號(hào) having 選修人數(shù) >2 order by 選修人數(shù) desc,課程號(hào) asc;
8、查詢兩門以上不及格課程的同學(xué)的學(xué)號(hào)及其平均成績(jī)

select 學(xué)號(hào),avg(成績(jī)) as 平均成績(jī) from score where 成績(jī) <60 group by 學(xué)號(hào) having count(課程號(hào))>=2;

?

復(fù)雜查詢:

沒(méi)有外鍵考慮子查詢
1、查詢所有課程成績(jī)小于60分學(xué)生的學(xué)號(hào)、姓名
select 學(xué)號(hào),學(xué)生姓名 from student where 學(xué)號(hào) in (select 學(xué)號(hào) from score where 成績(jī)<60);

2、查詢沒(méi)有學(xué)全所有課的學(xué)生的學(xué)號(hào)、姓名
select 學(xué)號(hào),學(xué)生姓名 from student where 學(xué)號(hào) in (select 學(xué)號(hào) from score group by 學(xué)號(hào) having count(課程號(hào))<3);

3、查詢出只選修了兩門課程的全部學(xué)生的學(xué)號(hào)和姓名
select 學(xué)號(hào),學(xué)生姓名 from student where 學(xué)號(hào) in (select 學(xué)號(hào) from scroe group by 學(xué)號(hào) having count(課程號(hào))=2);

4、1990年出生的學(xué)生名單
select 學(xué)生姓名 from student where 出生年月 like '1990%';

5、查詢各科成績(jī)前兩名的記錄 (select * from score where 課程號(hào) = 1 order by 成績(jī) desc limit 2)

union all

(select * from score where 課程號(hào) = 1 order by 成績(jī) desc limit 2)

union all

(select * from score where 課程號(hào) = 3 order by 成績(jī) desc limit 2);

多表查詢:

1、查詢所有學(xué)生的學(xué)號(hào)、姓名、選課數(shù)、總成績(jī)

select student.學(xué)號(hào),student.學(xué)生姓名,count(score.課程號(hào))as 選課數(shù),sum(score.成績(jī)) from?
student left join score on student.學(xué)號(hào)=score.學(xué)號(hào) GROUP BY student.學(xué)號(hào);

2、查詢平均成績(jī)大于85的所有學(xué)生的學(xué)號(hào)、姓名和平均成績(jī)
select student.學(xué)號(hào),student.學(xué)生姓名,avg(score.成績(jī))as 平均成績(jī) from student left join score on
student.學(xué)號(hào)=score.學(xué)號(hào) group by score.學(xué)號(hào) having avg(score.成績(jī))>85;

3、查詢學(xué)生的選課情況:學(xué)號(hào),姓名,課程號(hào),課程名稱
select student.學(xué)號(hào),student.學(xué)生姓名,score.課程號(hào),course.課程名稱 from student,score,course
where student.學(xué)號(hào)=score.學(xué)號(hào) and score.課程號(hào)=course.課程號(hào) ;

或者 select student.學(xué)號(hào),student.學(xué)生姓名,score.課程號(hào),course.課程名稱 from student inner join score on
student.學(xué)號(hào)=score.學(xué)號(hào) inner join course on score.課程號(hào)=course.課程號(hào) ; ? 4、查詢出每門課程的及格人數(shù)和不及格人數(shù)
select 課程號(hào) ,count(學(xué)號(hào)) as 及格人數(shù) from score where 成績(jī) >=60 group by 課程號(hào)

union all

select 課程號(hào) ,count(學(xué)號(hào)) as 不及格人數(shù) from score where 成績(jī) <60 group by 課程號(hào);

-- 考察case表達(dá)式 select 課程號(hào), sum(case when 成績(jī)>=60 then 1 else 0 end) as 及格人數(shù), sum(case when 成績(jī) < 60 then 1 else 0 end) as 不及格人數(shù) from score group by 課程號(hào); 5、查詢課程編號(hào)為0003且課程成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名|

select student.學(xué)號(hào),student.學(xué)生姓名 from student,score where
student.學(xué)號(hào)=score.學(xué)號(hào) and score.課程號(hào)=3 and score.成績(jī)>=80;

或者

select student.學(xué)號(hào),student.學(xué)生姓名 from student inner join score on student.學(xué)號(hào)=score.學(xué)號(hào) where score.課程號(hào)=3 and score.成績(jī)>=80;

多表查詢 where 在 on 的后面

?sql面試題:行列如何互換:

要替換成的結(jié)果為:

使用case表達(dá)式,替換常量列為對(duì)應(yīng)的成績(jī)

select 學(xué)號(hào),
(case when 課程號(hào)=1 then 成績(jī) else 0 end) as 課程號(hào)1, (case when 課程號(hào)=2 then 成績(jī) else 0 end) as 課程號(hào)2, (case when 課程號(hào)=3 then 成績(jī) else 0 end) as 課程號(hào)3 from score;

第3關(guān),分組

分組,并使用最大值函數(shù)max取出上圖每個(gè)方塊里的最大值

?

select 學(xué)號(hào),

max(case 課程號(hào) when 1 then 成績(jī) else 0 end) as 課程號(hào)1,

max(case 課程號(hào) when 2 then 成績(jī) else 0 end) as 課程號(hào)2,

max(case 課程號(hào) when 3 then 成績(jī) else 0 end) as 課程號(hào)3

from score group by 學(xué)號(hào);

?

轉(zhuǎn)載于:https://www.cnblogs.com/foremostxl/p/11141018.html

總結(jié)

以上是生活随笔為你收集整理的mysql命令查询语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。