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