標籤:
select *from lol--top 關鍵字select top 3 *from lol--查詢前三行 select top 3 *from lol where age>22 --加入條件select top 3 name,wuqi from lol where age>22 ----關鍵字 distinct 查詢不同項 去重select distinct name from lol --查詢不同名字的行--order by 升序select *from lol order by age asc--升序select *from lol order by age desc--降序 預設不寫的話是升序select *from lol order by age,sex --在年齡基礎上再按性別排 不改變第一組的結果基礎上,再按第二組進行排序--分組 group by+列--對那一列進行分組 就只能顯示哪一列select age from lol group by age --對某一列分組 相當於去重顯示 -- select 列 from lol group by 列go--運算子 查詢年齡加5歲的結果select * from lol where age+5>27--算術運算子 +-*/%--比較子 >< >= <= != <> !> !<--邏輯運算子 and or not(修飾符,不能單獨用)--修飾符 all any some in not--all 所有的意思 結合一個範圍來使用--in 在什麼範圍之內select * from lol where age in (22,23)--代表著等於第一個參數 or 等於第二個參數select * from lol where age not in (22,23) --年齡不在這個範圍之內 not 修飾作用--查詢年齡不在(身高是164的人的年齡)範圍之內的學生資訊--select * from student where high=164--select * from student where age!=23--子查詢:使用查詢語句查詢一列資料出來,然後作為其他查詢的查詢條件的參數來使用--查詢身高不在(年齡是22歲的人身高)範圍之內的學生資訊--select * from student where high not in(select high from student where age=22)--注意是一列的資訊作為參數go--外鍵:受約束的表叫外鍵表,約束的表叫主鍵表--要加外鍵,首先得有主鍵表--要刪除主鍵表的資料,必須先刪除外鍵表的資料--作為外鍵的資料來源的列,必須是一個唯一鍵(必須是主鍵或者是unique)create table teacher(tno int primary key not null,tname varchar)create table student1(sno int primary key,sname varchar,tno int references teacher(tno),--受teacher表中tno約束cid varchar(50) unique --不能重複的)
alter table lol add high int --添加一列 允許為空白alter table lol drop column zsx -- 刪除一列select *from lolupdate lol set guishudi=‘全真教‘ where code=2insert into lol values(‘人王伏羲‘,‘男‘,35,‘西王母‘,‘伏羲八卦‘,‘不周山‘,185)select *from lol where age>=25 and high>=180select * from lol order by high descselect*from lol where age not in(select age from lol where high=188)select name from lol where high=(select max(high) from lol )--身高最高的人的名字select COUNT(*)from lol --表中有多少條資料exec sp_rename‘lol.duixiang‘,‘wife‘,‘column‘ --修改列名--格式是 exec sp_rename‘表.原列名‘,‘新列名‘,‘column‘
SQL 學習筆記