mysql命令查询语句
?
?
1、單表查詢
select * from student; 采用*效率低,不推薦,多用列名 一、單表查詢的語法:SELECT 字段1,字段2... FROM 表名WHERE 條件GROUP BY fieldHAVING 篩選ORDER BY fieldLIMIT 限制條數(shù)二、關(guān)鍵字的執(zhí)行優(yōu)先級: fromwheregroup byhavingselectdistinct 去重處理order bylimit補充說明:
#查詢使用別名:
#查詢過濾重復(fù)
#連接查詢
2、多表查詢
交叉連接:不適用任何匹配條件。生成笛卡爾積 內(nèi)連接:只連接匹配的行 外鏈接之左連接:優(yōu)先顯示左表全部記錄 外鏈接之右連接:優(yōu)先顯示右表全部記錄 全外連接:顯示左右兩個表全部記錄# 分頁limit
# 聚合函數(shù)
sum返回一列的總和
#MySQL教程之concat以及group_concat的用法
一、concat()函數(shù)1、功能:將多個字符串連接成一個字符串。2、語法:concat(str1, str2,...)返回結(jié)果為連接參數(shù)產(chǎn)生的字符串,如果有任何一個參數(shù)為null,則返回值為null。 select concat (id, name, score) as info from tt2;?
?
group_concat() 1、功能:將group by產(chǎn)生的同一個分組中的值連接起來,返回一個字符串結(jié)果。 2、語法:group_concat( [distinct] 要連接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] ) 說明:通過使用distinct可以排除重復(fù)值;如果希望對結(jié)果中的值進行排序,可以使用order by子句;separator是一個字符串值,缺省為一個逗號。3、舉例:例7:使用group_concat()和group by顯示相同名字的人的id號:?#合并
?#注意 union與union all的區(qū)別:union會去掉相同的紀錄
?
# 通配符
#exists
EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時,內(nèi)層查詢語句不返回查詢的記錄。 而是返回一個真假值。True或False 當返回True時,外層查詢語句將進行查詢;當返回值為False時,外層查詢語句不進行查詢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)合唯一,比如同一個學(xué)生不能選重復(fù)的課程unique(student_id,course_id),
unique與primary key的區(qū)別:
簡單的講,primary key=unique+not null具體的區(qū)別:(1) 唯一性約束所在的列允許空值,但是主鍵約束所在的列不允許空值。(2) 可以把唯一性約束放在一個或者多個列上,這些列或列的組合必須有唯一的。但是,唯一性約束所在的列并不是表的主鍵列。(3) 唯一性約束強制在指定的列上創(chuàng)建一個唯一性索引。在默認情況下,創(chuàng)建唯一性的非聚簇索引,但是,也可以指定所創(chuàng)建的索引是聚簇索引。(4) 建立主鍵的目的是讓外鍵來引用.(5) 一個表最多只有一個主鍵,但可以有很多唯一鍵聯(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é)號,學(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é)號 int primary key ,學(xué)生姓名 char,出生年月 date,性別 enum('男','女'))2、成績表:score(學(xué)號,課程號,成績)
錯誤寫法:create table score(學(xué)號 int primary key ,課程號 int,成績 float,unique(學(xué)號,課程號))
這樣設(shè)置表就沒有主鍵了 正確寫法: 聯(lián)合主鍵:create table score(學(xué)號 int,課程號 int,成績 float,primary key(學(xué)號,課程號));
3、課程表:course(課程號,課程名稱,教師號)
create table course(課程號 int primary key,課程名稱 char,教師號 int)4、教師表:teacher(教師號,教師姓名)
create table teacher(教師號 int primary key,教師姓名 char)?插入數(shù)據(jù):在插入數(shù)據(jù)前用navicat或者sql語句檢查一下各字段的字符長度
desc student;?
(1)向?qū)W生表中
insert into student(學(xué)號,學(xué)生姓名,出生年月,性別) values(1,'猴子','1989-01-01','男'), (2 , '猴子' , '1990-12-21' , '女'),(3 , '馬云' , '1991-12-21' , '男'),
(4, '王思聰' , '1990-05-20' , '男');
(2)成績表
insert into score(學(xué)號,課程號,成績) 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(課程號,課程名稱,教師號) values(1,'語文',2), (2,'數(shù)學(xué)',1),(3,'英語',3);(4)教師表
insert into teacher(教師號,教師姓名) values(1,'孟扎扎'), (2,'馬化騰'),(3,null),(4,'');查詢語句
1、查詢姓‘猴’的學(xué)生名單select 學(xué)生姓名 from student where 學(xué)生姓名 like '猴%';
2、查詢姓名中最后一個字是‘猴’的學(xué)生名單
select 學(xué)生姓名 from student where 學(xué)生姓名 like '%猴'; 3、查詢姓名中帶‘猴’的學(xué)生名單 select 學(xué)生姓名 from student where 學(xué)生姓名 like '%猴%';
‘猴%’匹配以猴字開頭的? ?猴 后面有沒有字符無所謂? % 任意多個字符
‘猴_’匹配 以猴字開頭 兩個字符? ? ? ? ? ? ? ? ?_ 任意一個字符
匯總分析:
1、查詢課程編號為2的總成績 select sum(成績) as 課程編號為2總成績 from score where 課程編號=2;
2、查詢選了課程的學(xué)生人數(shù)? select count(distinct 學(xué)號) as 選課人數(shù) from score;
?分組:
1、查詢各科成績的最高分和最低分select 課程號,max(成績),min(成績) from score group by 課程號;
2、查詢每門課程被選修的學(xué)生數(shù)
select 課程號,count(學(xué)號) from score group by 課程號;
3、查詢男生、女生人數(shù)
sum是求和,count是計數(shù)
select 性別, count(*) from student GROUP BY 性別;
分組結(jié)果的條件
1、查詢平均成績大于60分學(xué)生的學(xué)號和平均成績select 學(xué)號 ,avg(成績) from score group by 學(xué)號 having avg(成績)>60 ;
2、查詢至少選修兩門課程的學(xué)生學(xué)號
select 學(xué)號 from score group by 學(xué)號 having count(學(xué)號)>=2;
3、查詢同名同性學(xué)生名單并統(tǒng)計同名人數(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、查詢不及格的課程并按課程號從大到小排列select 課程號,成績 from score where 成績 <60 order by 課程號 desc;
5、查詢每門課程的平均成績,結(jié)果按平均成績升序排序,平均成績相同時,按課程號降序排列
select 課程號,avg(成績) as 平均成績 from score group by 課程號 order by 平均成績 asc,課程號 desc;
6、檢索課程編號為“0004”且分數(shù)小于60的學(xué)生學(xué)號,結(jié)果按按分數(shù)降序排列
select 學(xué)號 from score where 課程號=4 and 成績<60 order by 成績 desc;
7、統(tǒng)計每門課程的學(xué)生選修人數(shù)(超過2人的課程才統(tǒng)計)要求輸出課程號和選修人數(shù),查詢結(jié)果按人數(shù)降序排序,若人數(shù)相同,按課程號升序排序
select 課程號 ,count(學(xué)號) as 選修人數(shù) from score group by 課程號 having 選修人數(shù) >2 order by 選修人數(shù) desc,課程號 asc; 8、查詢兩門以上不及格課程的同學(xué)的學(xué)號及其平均成績
select 學(xué)號,avg(成績) as 平均成績 from score where 成績 <60 group by 學(xué)號 having count(課程號)>=2;
?
復(fù)雜查詢:
沒有外鍵考慮子查詢1、查詢所有課程成績小于60分學(xué)生的學(xué)號、姓名
select 學(xué)號,學(xué)生姓名 from student where 學(xué)號 in (select 學(xué)號 from score where 成績<60);
2、查詢沒有學(xué)全所有課的學(xué)生的學(xué)號、姓名
select 學(xué)號,學(xué)生姓名 from student where 學(xué)號 in (select 學(xué)號 from score group by 學(xué)號 having count(課程號)<3);
3、查詢出只選修了兩門課程的全部學(xué)生的學(xué)號和姓名
select 學(xué)號,學(xué)生姓名 from student where 學(xué)號 in (select 學(xué)號 from scroe group by 學(xué)號 having count(課程號)=2);
4、1990年出生的學(xué)生名單
select 學(xué)生姓名 from student where 出生年月 like '1990%';
5、查詢各科成績前兩名的記錄 (select * from score where 課程號 = 1 order by 成績 desc limit 2)
union all
(select * from score where 課程號 = 1 order by 成績 desc limit 2)
union all
(select * from score where 課程號 = 3 order by 成績 desc limit 2);
多表查詢:
1、查詢所有學(xué)生的學(xué)號、姓名、選課數(shù)、總成績select student.學(xué)號,student.學(xué)生姓名,count(score.課程號)as 選課數(shù),sum(score.成績) from?
student left join score on student.學(xué)號=score.學(xué)號 GROUP BY student.學(xué)號;
select student.學(xué)號,student.學(xué)生姓名,avg(score.成績)as 平均成績 from student left join score on
student.學(xué)號=score.學(xué)號 group by score.學(xué)號 having avg(score.成績)>85;
3、查詢學(xué)生的選課情況:學(xué)號,姓名,課程號,課程名稱
select student.學(xué)號,student.學(xué)生姓名,score.課程號,course.課程名稱 from student,score,course
where student.學(xué)號=score.學(xué)號 and score.課程號=course.課程號 ;
或者 select student.學(xué)號,student.學(xué)生姓名,score.課程號,course.課程名稱 from student inner join score on
student.學(xué)號=score.學(xué)號 inner join course on score.課程號=course.課程號 ; ? 4、查詢出每門課程的及格人數(shù)和不及格人數(shù)
select 課程號 ,count(學(xué)號) as 及格人數(shù) from score where 成績 >=60 group by 課程號
union all
select 課程號 ,count(學(xué)號) as 不及格人數(shù) from score where 成績 <60 group by 課程號;
-- 考察case表達式 select 課程號, sum(case when 成績>=60 then 1 else 0 end) as 及格人數(shù), sum(case when 成績 < 60 then 1 else 0 end) as 不及格人數(shù) from score group by 課程號; 5、查詢課程編號為0003且課程成績在80分以上的學(xué)生的學(xué)號和姓名|
select student.學(xué)號,student.學(xué)生姓名 from student,score where
student.學(xué)號=score.學(xué)號 and score.課程號=3 and score.成績>=80;
或者
select student.學(xué)號,student.學(xué)生姓名 from student inner join score on student.學(xué)號=score.學(xué)號 where score.課程號=3 and score.成績>=80;多表查詢 where 在 on 的后面
?sql面試題:行列如何互換:
要替換成的結(jié)果為:
使用case表達式,替換常量列為對應(yīng)的成績select 學(xué)號,
(case when 課程號=1 then 成績 else 0 end) as 課程號1, (case when 課程號=2 then 成績 else 0 end) as 課程號2, (case when 課程號=3 then 成績 else 0 end) as 課程號3 from score;
第3關(guān),分組
分組,并使用最大值函數(shù)max取出上圖每個方塊里的最大值
?
select 學(xué)號,max(case 課程號 when 1 then 成績 else 0 end) as 課程號1,
max(case 課程號 when 2 then 成績 else 0 end) as 課程號2,
max(case 課程號 when 3 then 成績 else 0 end) as 課程號3
from score group by 學(xué)號;
?
轉(zhuǎn)載于:https://www.cnblogs.com/foremostxl/p/11141018.html
總結(jié)
以上是生活随笔為你收集整理的mysql命令查询语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: svn查看登录过的账号密码
- 下一篇: Linux记录-mysql参数优化