sql學習日記2

2021-10-01 08:14:17 字數 3571 閱讀 2673

1.查詢列資料

select * from 表名 [as 別名] 

-- as為表起別名,as可以省略

-- * 表示查詢所有列,若查詢指定列,改為列名,列名之間用逗號分隔,也可用as為列起別名(最好不用中文作為別名)

select

*from userinfo as ui

select username,userpwd from userinfo

select ui.username name,ui.userpwd as pwd from userinfo ui

查詢前n部分資料

select top n 列名 from 表名:表示檢視前n行

select top n percent 列名 from 表名:表示檢視前百分之幾的資料

select

top1

*from subject

select

top2

percent

*from subject

排序

--asc表示公升序,desc表示降序

--可以設定多個條件排序,按照先後順序優先排序

select * from 表名 order by 列名 asc/desc[,列名,asc/desc]

消除重複行

select distinct 列名 from 表名
條件查詢:寫在where後面

對行進行篩選,返回bool型別的值,如果某行中的列資料滿足條件,則載入出來

select

*from score where sid >=

2

select

*from score where scorevalue between

60and

80

select

*from score where sid in(1

,2,5

)

select

*from score where sid=

1or sid=

3and cid=

1

%:0到多個任意字元;    _:乙個任意字元;   :乙個某範圍內的字元

%與_寫在中表示本身的含義

在表示乙個連續的範圍可以使用 -,如[0-9]

^寫在內部的開頭,表示不使用內部的任何字元

--名字中包含三的學生資訊

select

*from studentinfo where sname like

'%三%'

--姓張的學生

select

*from studentinfo where sname like

'張%'

--姓名為2個字的姓王的學生

select

*from studentinfo where sname like

'王_'

--查詢使用**為15-19段的學生資訊

select

*from studentinfo where sphone like

'1[5-9]%'

--查詢使用**為10-14段的學生資訊

select

*from studentinfo where sphone like

'1[^5-9]%'

小括號》not>比較運算子》邏輯運算子
連線查詢

join 表名 on 關聯條件

內連線:inner join,兩表中完全匹配的資料

左外連線:left outer join,兩表中完全匹配的資料,左表中特有的資料

右外連線:left outer join,兩表中完全匹配的資料,右表中特有的資料

完全外連線:full outer join,

當需要的結果從多張表中取時

關鍵問題:涉及的表,關係

--查詢學生姓名及所在班級名稱

--studentinfo

--classinfo

--關係:studentinfo.cid=>classinfo.cid

select si.sname,ci.title

from studentinfo as si

inner

join classinfo as ci on si.cid=ci.cid

insert into 表名(列名)values(值)
說明:

--插入(1)

insert userinfo(username,userpwd)

values

('麗麗'

,'21232f297a57a5a'

)--插入(2):為所有列按照預設順序賦值

insert userinfo

values

('朵朵'

,'21232f297a57a5a74'

)--插入(3):為某些列賦值

insert userinfo(username)

values

('可可'

)--一次性些多個資料

select

*from classinfo

insert

into classinfo values

('1班'),

('2班'),

('3班'

)

updata 表名 set 列名1=值1,列名2=值2…where…
--為指定行修改某列資料

update userinfo set userpwd =

'admin'

where username =

'麗麗'

update userinfo set userpwd =

'admin'

where username !=

'麗麗'

--若是要修改某列的資料,刪除where及其以後內容

update userinfo set userpwd =

'null'

delete from 表名 where
from可以省略不寫

刪除資料後,主鍵值會繼續往後增加,不連續

清空(重置)

truncate table 表名
注:若刪除內容作為其他表的外來鍵,刪除會報錯

set statistics time on/of:在訊息欄顯示詳細執行時間

ctrl+e:執行

ctrl+r:隱藏訊息欄

ctrl+l:計畫任務,對sql語句進行分析,了解語句執行耗能,從而優化語句

學習日記2

陣列的初始化,一維陣列和二維陣列的定義以及使用,字元陣列的定義和使用,函式的形式引數和實際引數,全域性變數和區域性變數,變數的儲存類別,預處理命令。今日任務完成情況 本日任務按照老師要求完成 今日開發中出現的問題彙總 編譯程式的出錯,演算法結構的不正確和不完整,以往的冒泡函式沒有記牢,對於陣列元素和...

php學習日記2

define name,value,case insensitive 定義乙個常量,常量與變數的區別在於不用加 而且常量的值無法被更改。語法為,define name 常量名 value 常量值 case insensitive 大小寫敏感 預設為false,即對大小寫敏感,可以設定為true,對大...

Git學習日記 2

建立ssh key 命令 ssh keygen t rsa c youremail example.com 登入github 兩個引數 title,key title隨便輸,key的值為linux當前使用者目錄下.ssh資料夾下id rsa.pub檔案下的內容 在github上新建乙個learngi...