首先借用Freesql的官方文档中的一段复杂代码举例,说明SQL语句的基本使用并最终实现读懂下列的复杂代码
INSERT INTO [Role]([Name]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[Name] as [Name] VALUES(N'role1'), (N'role2')
INSERT INTO [User]([Name]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[Name] as [Name] VALUES(N'user1')
INSERT INTO [UserExt]([UserId]) VALUES(1)
INSERT INTO [UserExtRemarks]([RemarkId], [UserId], [Remark]) VALUES('6570e3f8-a226-c3ac-00d1-a3dd18b30339', 1, N'remark1'), ('6570e3f8-a226-c3ac-00d1-a3de16d9aa68', 1, N'remark2')
INSERT INTO [UserClaim]([UserId], [ClaimName]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[UserId] as [UserId], INSERTED.[ClaimName] as [ClaimName] VALUES(1, N'claim1'), (1, N'claim2'), (1, N'claim3')
INSERT INTO [UserRole]([UserId], [RoleId]) VALUES(1, 5), (1, 6)
INSERT INTO [Role]([Name]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[Name] as [Name] VALUES(N'role111100001'), (N'role2')
INSERT INTO [UserClaim]([UserId], [ClaimName]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[UserId] as [UserId], INSERTED.[ClaimName] as [ClaimName] VALUES(1, N'claim0000')
INSERT INTO [UserRole]([UserId], [RoleId]) VALUES(1, 7), (1, 8)
DELETE FROM [UserRole] WHERE ([UserId] = 1 AND [RoleId] = 6)
DELETE FROM [UserRole] WHERE ([UserId] = 1 AND [RoleId] = 5)
DELETE FROM [UserClaim] WHERE ([Id] = 2)
UPDATE [User] SET [Name] = N'user1111'
WHERE ([Id] = 1)
UPDATE [UserClaim] SET [ClaimName] = CASE [Id]
WHEN 1 THEN N'claim1111'
WHEN 3 THEN N'claim3222222' END
WHERE ([Id] IN (1,3))
DELETE FROM [UserRole] WHERE (([UserId] = 1 AND [RoleId] = 7) OR ([UserId] = 1 AND [RoleId] = 8))
DELETE FROM [UserClaim] WHERE ([Id] IN (1,3,4))
DELETE FROM [UserExt] WHERE ([UserId] = 1)
DELETE FROM [User] WHERE ([Id] = 1)
为了便于理解可以将语句拆分成很多部分,例如这两句将Value为role1和role2的值插入进Role的表和将Value为user1的值插入进User的表,并output出插入的id和name
INSERT INTO [Role]([Name]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[Name] as [Name] VALUES(N'role1'), (N'role2')
INSERT INTO [User]([Name]) OUTPUT INSERTED.[Id] as [Id], INSERTED.[Name] as [Name] VALUES(N'user1')
下面的SQL语句实现了将值为1的UserId插入进UserExt中和将([RemarkId], [UserId], [Remark])的值为('6570e3f8-a226-c3ac-00d1-a3dd18b30339', 1, N'remark1'), ('6570e3f8-a226-c3ac-00d1-a3de16d9aa68', 1, N'remark2')的两组插入进表UserExtRemarks中,其他INSERT语句含义相似,所以不再过多解释
INSERT INTO [UserExt]([UserId]) VALUES(1);
INSERT INTO [UserExtRemarks]([RemarkId], [UserId], [Remark])
VALUES('6570e3f8-a226-c3ac-00d1-a3dd18b30339', 1, N'remark1'),
('6570e3f8-a226-c3ac-00d1-a3de16d9aa68', 1, N'remark2');
下面的SQL语句实现了DELETE的操作,在UserRole表中将UserId为1,RoleId为5和6的值删除
DELETE FROM [UserRole] WHERE ([UserId] = 1 AND [RoleId] = 6)
DELETE FROM [UserRole] WHERE ([UserId] = 1 AND [RoleId] = 5)
UPDATE语句主要实现的是SQL语句中的更新操作,例如下面的一句,将User表中的ID为1的项的name改为user1111,同理下面是将UserClaim表中Id为1和3的Claimname分别改为claim1111和claim3222222
UPDATE [User] SET [Name] = N'user1111'
WHERE ([Id] = 1)
UPDATE [UserClaim] SET [ClaimName] = CASE [Id]
WHEN 1 THEN N'claim1111'
WHEN 3 THEN N'claim3222222' END
WHERE ([Id] IN (1,3))
下面同样是SQL语句中的DELETE语句,执行删除,依靠where找到条件
DELETE FROM [UserRole] WHERE (([UserId] = 1 AND [RoleId] = 7) OR ([UserId] = 1 AND [RoleId] = 8));
DELETE FROM [UserClaim] WHERE ([Id] IN (1,3,4));
DELETE FROM [UserExt] WHERE ([UserId] = 1);
DELETE FROM [User] WHERE ([Id] = 1
下面是一些基础语法SQL SELECT DISTINCT 语句 | 菜鸟教程,SQL 快速参考其中摘取了一些
- SELECT - 从数据库中提取数据
- UPDATE - 更新数据库中的数据
- DELETE - 从数据库中删除数据
- INSERT - 向数据库中插入新数据
- WHERE - 用于条件过滤数据
- ALTER - 修改数据库或数据库表
- CREATE - 创建新数据库或表
- ORDER BY - 排序方式,
ASC
表示升序,DESC
表示降序 - DROP - 删除表
- INNER JOIN - 连接数据表,分为left和right
- AND OR NOT - 与 或 非
-
语句 语法 AND / OR SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR conditionALTER TABLE (add column) ALTER TABLE table_name
ADD column_name datatypeALTER TABLE (drop column) ALTER TABLE table_name
DROP COLUMN column_nameAS (alias for column) SELECT column_name AS column_alias
FROM table_nameAS (alias for table) SELECT column_name
FROM table_name AS table_aliasBETWEEN SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2CREATE DATABASE CREATE DATABASE database_name CREATE INDEX CREATE INDEX index_name
ON table_name (column_name)CREATE TABLE CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
.......
)CREATE UNIQUE INDEX CREATE UNIQUE INDEX index_name
ON table_name (column_name)CREATE VIEW CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE conditionDELETE FROM DELETE FROM table_name
(Note: Deletes the entire table!!)or
DELETE FROM table_name
WHERE conditionDROP DATABASE DROP DATABASE database_name DROP INDEX DROP INDEX table_name.index_name DROP TABLE DROP TABLE table_name GROUP BY SELECT column_name1,SUM(column_name2)
FROM table_name
GROUP BY column_name1HAVING SELECT column_name1,SUM(column_name2)
FROM table_name
GROUP BY column_name1
HAVING SUM(column_name2) condition valueIN SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)INSERT INTO INSERT INTO table_name
VALUES (value1, value2,....)or
INSERT INTO table_name
(column_name1, column_name2,...)
VALUES (value1, value2,....)LIKE SELECT column_name(s)
FROM table_name
WHERE column_name
LIKE patternORDER BY SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]SELECT SELECT column_name(s)
FROM table_nameSELECT * SELECT *
FROM table_nameSELECT DISTINCT SELECT DISTINCT column_name(s)
FROM table_nameSELECT INTO
(used to create backup copies of tables)SELECT *
INTO new_table_name
FROM original_table_nameor
SELECT column_name(s)
INTO new_table_name
FROM original_table_nameTRUNCATE TABLE
(deletes only the data inside the table)TRUNCATE TABLE table_name UPDATE UPDATE table_name
SET column_name=new_value
[, column_name=new_value]
WHERE column_name=some_valueWHERE SELECT column_name(s)
FROM table_name
WHERE condition
下面是一些常用的基本SQL语句的基本含义
create database; 创建数据库
use database; 打开数据库
SELECT * FROM table; 将选择表中全部数据,其中*可以被用作通配符,表示全部的列的含义,以下是一个典型的SQL语句SELECT,表示从table_name中选取column1等字段名称及选择的条件
SELECT column1, column2, ...
FROM table_name
[WHERE condition]
[ORDER BY column_name [ASC | DESC]]
[LIMIT number];
SELECT DISTINCT,指选取表内的不重复值,如有多个相同值,则只显示单个不同的值
WHERE 作为一个前置条件出现,限制了SQL语句的执行条件,基本运算符都可以使用,其中三个特殊的字符为:
BETWEEN | 在某个范围内 |
LIKE | 搜索某种模式 |
IN | 指定针对某个列的多个可能值 |
使用MySQL自带的数据库表city查询其中population在100000到110000之间的city,其中and表示范围之内
SELECT * FROM `city` WHERE Population BETWEEN 100000 and 110000
查询结果为,代码正常运行
27 Emmen NLD Drenthe 105853
28 Zwolle NLD Overijssel 105819
29 Ede NLD Gelderland 101574
49 Béchar DZA Béchar 107311
50 Tiaret DZA Tiaret 100118
142 Townsville AUS Queensland 109914
165 Brahmanbaria BGD Chittagong 109032
166 Tangail BGD Dhaka 106004
167 Jamalpur BGD Dhaka 103556
168 Pabna BGD Rajshahi 103277
169 Naogaon BGD Rajshahi 101266
181 Schaerbeek BEL Bryssel 105692
182 Namur BEL Namur 105419
189 Parakou BEN Borgou 103577
205 Francistown BWA Francistown 101805
同时,and还可以作为表示与的条件进行使用,表示countrycode = nld 的同时,population > 105000的全部city
SELECT * FROM `city` WHERE CountryCode = 'NLD' AND Population > 105000
order by 可作为排序,如下表示按人口降序排序
SELECT * FROM `city` ORDER BY Population DESC
该语句实现了查询users表内的第一个成员,并返回name和password
SELECT `name`,`password`
FROM users
LIMIT 1;
按population升序,选择前50的city
SELECT * FROM city
ORDER BY Population ASC
LIMIT 50
在user表中插入一个name 为sawaa,password 为111的用户,实现了插入功能
INSERT INTO users (`name` , `password`)
VALUES ('sawaa' , 111)
新建了一个数据库表,users,其中表里新建了三条用户来实现基本的SQL功能,分别为:
1 sawaa 111
2 pvah 222
3 tailuo 333
下面该语句实现了基本的更新
UPDATE users
SET name = 'Lihua' , `password` = 192
WHERE id = 3
运行后结果为
3 Lihua 192
2 pvah 222
1 sawaa 111
DELETE FROM users WHERE `name` = 'Lihua'
该代码实现了删除user中名字为Lihua的用户
下面是很重要的模糊查询,主要用%符号表示,下列代码主要实现了查询city中所有name里含有ta字段的项目
SELECT * FROM city
WHERE `Name` LIKE '%ta%'
输出结果为
Zaanstad NLD Noord-Holland 135621
Willemstad ANT Curaçao 2345
Constantine DZA Constantine 443727
Mostaganem DZA Mostaganem 115212
Tafuna ASM Tutuila 5200
SELECT * FROM city
WHERE `Name` LIKE 'ta%'
这样写则是查询所有ta开头的项
SELECT * FROM city
WHERE `Name` LIKE 'ta_ _ _ _'
同理这样写则是确定了字母的个数(没有空格)
通配符直接借用菜鸟教程的表格SQL 通配符 | 菜鸟教程
通配符 | 描述 |
---|---|
% | 替代 0 个或多个字符 |
_ | 替代一个字符 |
[charlist] | 字符列中的任何单一字符 |
[^charlist] 或 [!charlist] | 不在字符列中的任何单一字符 |
下面的 SQL 语句选取 name 以 A 到 H 字母开头的city
SELECT * FROM city
WHERE name REGEXP '^[A-H]';
同理,下面的 SQL 语句选取 name 不为以 A 到 H 字母开头的city
SELECT * FROM city
WHERE name REGEXP '^[^A-H]';
IN 操作符允许在 WHERE 子句中规定多个值。
SELECT * FROM city
WHERE name IN ('Qandahar' , 'Utrecht')
并且可以同时应用
SELECT * FROM city
WHERE Population BETWEEN 100000 and 150000
AND CountryCode NOT IN ('AFG' , 'NLD')
标签:语句,name,column,UserId,理解,SQL,table,WHERE,Id From: https://blog.csdn.net/hhn6666/article/details/143584698