常用的几个SQL 查询语句
常用的几个SQL 查询语句
记录添加
Insert Into 表名 (字段1,字段2) Values (值1,值2)
INSERT INTO tbClass (ClassName) VALUES ('测试1')
INSERT INTO tbClass (ClassName) VALUES ('测试2')
INSERT INTO tbClass (ClassName) VALUES ('测试3')
INSERT INTO tbClass (ClassName) VALUES ('测试1')
INSERT INTO tbClass (ClassName) VALUES ('测试2')
INSERT INTO tbClass (ClassName) VALUES ('测试3')
记录修改
Update 表名 Set 字段1=值1,字段2=值2 Where 条件
UPDATE tbClass SET ClassName='修改1' WHERE ClassName='测试1'
UPDATE tbClass SET ClassName='修改2' WHERE ClassName='测试2'
UPDATE tbClass SET ClassName='修改1' WHERE ClassName='测试1'
UPDATE tbClass SET ClassName='修改2' WHERE ClassName='测试2'
记录删除
Delete From 表名 Where 条件
DELETE FROM tbUser WHERE UserGender=0
DELETE FROM tbUser WHERE UserID='20f3765f-593a-4ad6-8745-342b0b10828d'
DELETE FROM tbUser WHERE UserGender=0
DELETE FROM tbUser WHERE UserID='20f3765f-593a-4ad6-8745-342b0b10828d'
单表查询
Select 字段1,字段2 From 表名 Where 条件 Order By 排序字段1,排序字段2
多表查询
内连接 Select 字段1,字段2 From 表1 Inner Join 表2 ON 连接条件
左连接 Select 字段1,字段2 From 表1 Left Join 表2 ON 连接条件
右连接 Select 字段1,字段2 From 表1 Right Join 表2 ON 连接条件
SELECT tbBoard.BoardName,tbClass.ClassName From tbBoard INNER JOIN tbClass ON tbClass.ClassID=tbBoard.BoardClassID
左连接 Select 字段1,字段2 From 表1 Left Join 表2 ON 连接条件
右连接 Select 字段1,字段2 From 表1 Right Join 表2 ON 连接条件
SELECT tbBoard.BoardName,tbClass.ClassName From tbBoard INNER JOIN tbClass ON tbClass.ClassID=tbBoard.BoardClassID
聚合函数
SELECT * FROM TMP -- 查询所有记录
SELECT COUNT(*) FROM TMP -- 查询记录数
SELECT SUM(SCORE) FROM TMP -- 查询累加值
SELECT MIN(SCORE) FROM TMP -- 查询最小值
SELECT MAX(SCORE) FROM TMP -- 查询最大值
SELECT AVG(SCORE) FROM TMP -- 查询平均值
SELECT COUNT(*) FROM TMP -- 查询记录数
SELECT SUM(SCORE) FROM TMP -- 查询累加值
SELECT MIN(SCORE) FROM TMP -- 查询最小值
SELECT MAX(SCORE) FROM TMP -- 查询最大值
SELECT AVG(SCORE) FROM TMP -- 查询平均值