mysql未借出的書數 mySQL練習題

2021-10-17 22:20:42 字數 4227 閱讀 5758

1 create tablebook (2 bid char(10) not null,3 title char(50) default null,4 author char(20) default null,5 primary key(bid)6 );7 insert into book values ('b001', '人生若只如初見', '安意如');8 insert into book values ('b002', '入學那天遇見你', '晴空');9 insert into book values ('b003', '感謝折磨你的人', '如娜');10 insert into book values ('b004', '我不是教你詐', '劉庸');11 insert into book values ('b005', '英語四級', '白雪');12 create tableborrow (13 borrowid char(10) not null,14 stuid char(10) default null,15 bid char(10) default null,16 t_time varchar(50) default null,17 b_time varchar(50) default null,18 primary key(borrowid)19 ) ;20 insert into borrow values ('t001', '1001', 'b001', '2007-12-26 00:00:00', null);21 insert into borrow values ('t002', '1004', 'b003', '2008-01-05 00:00:00', null);22 insert into borrow values ('t003', '1005', 'b001', '2007-10-08 00:00:00', '2007-12-25 00:00:00');23 insert into borrow values ('t004', '1005', 'b002', '2007-12-16 00:00:00', '2008-01-07 00:00:00');24 insert into borrow values ('t005', '1002', 'b004', '2007-12-22 00:00:00', null);25 insert into borrow values ('t006', '1005', 'b005', '2008-01-06 00:00:00', null);26 insert into borrow values ('t007', '1002', 'b001', '2007-09-11 00:00:00', null);27 insert into borrow values ('t008', '1005', 'b004', '2007-12-10 00:00:00', null);28 insert into borrow values ('t009', '1004', 'b005', '2007-10-16 00:00:00', '2007-12-18 00:00:00');29 insert into borrow values ('t010', '1002', 'b002', '2007-09-15 00:00:00', '2008-01-05 00:00:00');30 insert into borrow values ('t011', '1004', 'b003', '2007-12-28 00:00:00', null);31 insert into borrow values ('t012', '1002', 'b003', '2007-12-30 00:00:00', null);32

33 create tablestudent (34 stuid char(10) not null,35 stuname varchar(10) default null,36 major varchar(50) default null,37 primary key(stuid)38 );39

40 insert into student values ('1001', '林林', '計算機');41 insert into student values ('1002', '白楊', '計算機');42 insert into student values ('1003', '虎子', '英語');43 insert into student values ('1004', '北漂的雪', '工商管理');44 insert into student values ('1005', '五月', '數學');45

46 /*

47 1. 查詢「計算機」專業學生在「2007-12-15」至「2008-1-8」時間段內借書的學生編號、學生名稱、圖書編號、圖書名稱、借出日期;48 2. 查詢所有借過圖書的學生編號、學生名稱、專業;49 3. 查詢沒有借過圖書的學生編號、學生名稱、專業;50 4. 查詢借過作者為「安意如」的圖書的學生姓名、圖書名稱、借出日期、歸還日期;51 5. 查詢借過書但有書未歸還的學生編號、學生名稱、圖書編號、圖書名稱、借出日期52 6. 查詢目前借書但未歸還圖書的學生名稱及未還圖書數量;53 */

54 --查詢「計算機」專業學生在「2007-12-15」至「2008-1-8」時間段內借書的學生編號、學生名稱、圖書編號、圖書名稱、借出日期;

55 select

56 t1.stuid,57 t1.stuname,58 t3.bid,59 t3.title,60 t2.t_time61 from

62 student t1,63 borrow t2,64 book t365 where

66 t1.stuid=t2.stuid and

67 t3.bid=t2.bid and

68 t1.major='計算機' and

69 t2.t_time between '2007-12-15' and '2008-1-8';70 --查詢所有借過圖書的學生編號、學生名稱、專業;

71 select

72 distinct

73 t2.stuid,74 t2.stuname,75 t2.major76 from

77 borrow t1,78 student t279 where

80 t1.stuid=t2.stuid81 --3. 查詢沒有借過圖書的學生編號、學生名稱、專業;

83 select

84 student.stuid,85 student.stuname,86 student.major87 from

88 student89 where student.stuid not in(90 select

91 t1.stuid92 from

93 borrow t1,94 student t295 where

96 t1.stuid=t2.stuid97 );98

99 select

100 t1.stuid101 from

102 borrow t1,103 student t2104 where

105 t1.stuid=t2.stuid;106 --4. 查詢借過作者為「安意如」的圖書的學生姓名、圖書名稱、借出日期、歸還日期;

107 select

108 student.stuname,109 book.title,110 borrow.t_time,111 borrow.b_time112 from

113 student,borrow,book114 where

115 student.stuid=borrow.stuid and

116 book.bid=borrow.bid and

117 book.author='安意如';118

119 --5. 查詢借過書但有書未歸還的學生編號、學生名稱、圖書編號、圖書名稱、借出日期

120 select

121 t1.stuid,122 t1.stuname,123 t3.bid,124 t3.title,125 t2.t_time126

127 from

128 student t1,129 borrow t2,130 book t3131 where

132 t1.stuid=t2.stuid and

133 t3.bid=t2.bid and

134 isnull(t2.b_time);135

136 --6. 查詢目前借書但未歸還圖書的學生名稱及未還圖書數量;

137 select

139 t1.stuid,140 t1.stuname,141 count(t2.bid)142 from

143 student t1,144 borrow t2,145 book t3146 where

147 t1.stuid=t2.stuid and

148 t3.bid=t2.bid and

149 isnull(t2.b_time)150 group byt2.stuid;151

查詢Mysql未使用的索引

在mysql中如何找出未使用或使用次數很少的索引,這樣文章比較多,但很少文章提到用這些方法存在的風險。這篇文章主要記錄,我對如何找未使用索引的理解及風險 目前還未找到理想方法 能像oracle儲存執行計畫,根據執行計畫 v sql plan 來判斷索引使用情況是比較安全。當然oracle的index...

php中mysql函式 PHP中的MySQL函式

本篇主要介紹採用php語言如何連線mysql資料庫。首先需要檢測服務mysql是否開啟成功。檢視phpinfo 函式 使用php運算元據庫的步驟 一 連線資料庫伺服器 二 選擇資料庫 mysql select db 資料庫名 三 設定編碼格式 mysql set charset utf 8 四 資料...

mysql可以分享的技術 技術分享 MySQL

1 查詢語句是如何執行的?1 連線 1 建立連線 2 驗證許可權,修改了許可權,建立新的連線才會生效。3 sql執行的臨時記憶體 2 查詢快取 1 先查詢快取,更新操作會導致所有快取失效。2 mysql 8.0功能去掉 3 分析 詞法解析,語法解析 4 優化 1 決定使用哪個索引,比方說根據統計資訊...