SQL常用命令總結(一)

增加

1、在表中插入數據

命令:insert into 表名 (列名 ,列名) values (值,值)
例如:insert into student (name,sex,age,grade) values (‘張三’,113,26)

刪除

1、刪除表格

命令:drop table 表名
例如:drop table student

修改

1、修改表中的數據

命令:update from 表名 set 列名=新值
例如:update from student set grade=36

查找

1、基礎查找表的數據

命令:select 要查詢的數據列名(*from 表名 where 篩選條件(無法對分組後的數據進行篩選)
例如:select name fron student where grade<30  //查找分數小於30分的學生名字
例如:select * fron student where grade<30  //查找分數小於30分的所有學生信息

2、高級查找表的數據

命令:select 要查詢的數據列名(*from 表名 where 篩選條件 group by 列名(分組)having 篩選條件(只能對分組後的數據進行篩選)order by 排序方式(控制數據最後輸出的排列方式有正序:asc、倒敘:desc)】
例如:通過性別分組,sex=1的正序排序後,查找分數小於30分的學生名字
select name fron student where grade<30 [group by sex having sex=1 order by asc]

**多條件查詢**
命令:其他相同,where後面的條件用and鏈接
例如:....... where age=13 and grade=100

3、多個表同時查找(內連接查詢)

命令:select 要查詢的數據列名(*) from 表名1,表名2 where 篩選條件(例如表名1.列明=表明2.列明)
例如:查找學生表和家長表,找到 學生表裏名字跟家長表裏的孩子列 對應的數據
select * from student,parent where student.name=parent.child

**使用別名**
命令:select 要查詢的數據列名(*) from 表名11別名,表名22別名 where 篩選條件(例如表名1別名.列明=表明2別名.列明)
例如:查找學生表和家長表,找到 學生表裏名字跟家長表裏的孩子列 對應的數據
select s.name,p.name from student s,parent p where s.name=p.child

4、其他關鍵字查詢(子查詢)

**帶In關鍵字的子查詢1**
命令:第一個select語句 did in (第二個select語句)
例如:查詢父母表裏年齡爲50歲的人孩子的數據,然後給外層的查詢語句使用。
select * from student where did in select child from parent where age=50

**帶In關鍵字的子查詢2**
命令:第一個select語句 did not in (第二個select語句)
例如:查詢父母表裏年齡****50歲的人孩子的數據,然後給外層的查詢語句使用。
select * from student where did not in select * from parent where age=50
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章