sql增刪改查之高階查詢

2021-08-11 08:27:41 字數 3646 閱讀 7618

1.連線查詢/多表查詢

假設有二張表t1和t2:

t1的字段(id,name,age)

t2的字段(id,class,score)

(1)查詢出學生的姓名,年齡和分數

mysql> select name,age,score

from t1 inner join t2 on t1.id = t2.id;

inner join 可以省略寫成join

注意:這就是內連線,返回的行都是二個表相匹配的資料

(2)mysql> select t1.id,name,age,score

from t1 left join t2 on t1.id = t2.id;

注意:這是左連線,除了返回二個表相匹配的資料,還會返回左表多餘的資料,與右表不匹配以null顯示

(3)mysql> select t1.id,name,age,score

from t1 right join t2 on t1.id = t2.id;

注意:這是右連線,除了返回二個表相匹配的資料,還會返回右表多餘的資料,與左表不匹配以null顯示

注意:mysql中只有內連線、左連線和右連線,沒有全連線

內連線返回二個表關聯字段共同都有的資料

左連線除了返回內連線的資料,還返回左表多餘的資料,左連線只要記住左表為大

右連線除了返回內連線的資料,還返回右表多餘的資料,右連線只要記住右表為大

(4)假設是三表連線,內連線如下寫法,其它連線相似

select * from t1 join t2 on t1.id= t2.id join t3 on t2.id = t3.id;

上面的寫法等於下面這種寫法:

==select * from t1,t2,t3

where t1.id = t2.id and t2.id =t3.id;

王豆豆更喜歡下面這種寫法,更簡單,更快。

接下的多表查詢都將使用這種方法。

現在有三張表,表結構如下:

班級(class)表(學號(id),姓名(name),班級(class),年齡(age))

課程(course)表(課程名(cname),課程號(cno))

分數(score)表(學號(xuehao),課程號(scno),分數(score))

(5)查詢出學員的姓名,班級,課程名以及相應課程的分數,並以分數進行降序排列

select c.name,c.class,co.cname,s.score

from class c,course co,score s

where c.id = s.xuehao and co.cno = s.scno

order by score desc;

(6)查詢出學員姓名以張開頭的學員姓名,課程,分數

select c.name,c.class,co.cname,s.score

from class c,course co,score s

where c.id = s.xuehao and co.cno = s.scno and c.name like 『張%』;

(7)查詢學員分數大於70的學員姓名,班級,課程

select c.name,class,cname

from class c,course co, score s

where c.id = s.xuehao and co.cno=s.scno and s.score > 『70』;

(8)查詢學員班級為t1001的學員課程號

select s.scno

from class c,score s

where c.id = s.xuehao and c.class = 『t1001』;

(9)查詢學員姓名包含王的學員分數,並以降序的方式排列

select s.score

from class c,score s

where c.id = s.xuehao and c.name like 『%王%』

order by score desc;

(10)查詢年齡不在25到27之間的學員姓名,課程編號,分數

解題步驟:

1.查詢出年齡不在25到27之間學員資訊

select * from class where age not between 25 and 27;

2.查詢出學員的姓名,課程編號,分數

select name,scno,score from class c,score s

where c.xuehao = s.xuehao;

3.合併

select name,scno,score from class c,score s

where c.xuehao = s.xuehao and (age not between 25 and 27);

2.子查詢

乙個查詢語句包含其他的查詢語句,則叫子查詢

子查詢有幾種結果:

子查詢返回一列語句:也就是一列乙個資料,這是使用情況最多的

返回一行語句:查詢結果返回一行資料,有可能是一行完整的資料

返回多行語句:查詢結果返回一組資料

格式:select [all|distinct] [as alias1]| [as alias2],….}

from table1 [別名],table2,{

select [all|distinct] [as alias1]| [as alias2],….}

from table1 [別名],table2,….

[where 某列名稱 = 某值]

[order by …]

}別名,….

[where (條件s) [as alias1]| [as alias2],….}

from table1 [別名],table2,….

[where 某列名稱 = 某值]

[order by …]

[order by …]

(1)查詢出分數大於tester測試基礎的分數的學員姓名和分數

解題步驟:

1.查詢tester測試基礎的分數

select score

from score s,course c

where s.scno = c.cno and c.cname =』測試基礎』 and s.name = 『tester』;

2.查詢出分數大於1題查詢結果的學員姓名和分數

select name,score

from score

where score > (select score

from score s,course c

where s.scno = c.cno and c.cname =』測試基礎』 and s.name = 『tester』);

(2)查詢出分數小於測試基礎最低分數的學員姓名和課程,分數

解題步驟:

1.查詢出測試基礎的分數(查詢結果為一組資料)

select score

from score s,course c

where s.scno = c.cno and c.cname =』測試基礎』;

2.查詢出分數小於最低分數的學員姓名和課程,分數

select s.name,c.cname,s.score

from score s,course c

where s.scno = c.cno and score

SQL 增刪改查

之前大致了解過,現在用 mysql 的還是居於多數,而且自己之後也有意嚮往大前端發展,所以就需要撿起以前的 sql,也希望將來有機會用 node.js mysql 做大型專案的機會。因此,就從簡單的 sql 的增刪改查開始大前端之路。開發中最常見的就是 select 查詢。簡單的查詢,看起來是這樣的...

SQL增刪改查

1 增 insert into table name values value1,value2,insert into table name 列1,列2,values 值1,值2,2 刪 delete from table name where 列名稱 值 3 改 update table name...

sql增刪改查語法

1.使用insert插入單行資料 語法 insert into 表名 列名 values 列值 例 insert into strdents 姓名,性別,出生日期 values 斌 男 1993 6 15 注意 into可以省略 列名列值用逗號分開 列值用單引號因上 如果省略表名,將依次插入所有列 ...