SQL Server (Transact-SQL) FROM 关键字用于指定应从中选择或删除数据的表。
示例:
考虑一个名为 Employee 的数据库表,其中包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
选择姓名Employee表中存在员工的和Salary数据,可以使用以下查询:
SELECT Name, Salary FROM Employee;
This将产生如下所示的结果:
Name | Salary |
---|---|
John | 3000 |
Marry | 2750 |
Jo | 2800 |
Kim | 3100 |
Ramesh | 3000 |
Huang | 2800 |
要选择 Employee 表的所有字段,查询将为:
SELECT * FROM Employee;
以下代码的结果将是:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
要删除 EmpID 为 5 的员工的记录,查询将是:
DELETE FROM Employee
WHERE EmpID = 5;
--查看结果
SELECT * FROM Employee
现在Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |
类似地,删除 city 以"New"开头的员工记录,查询将为:
DELETE FROM Employee
WHERE City LIKE 'New%';
-- 查看结果
SELECT * from Employee;
现在Employee表将包含以下记录:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
6 | Huang | Beijing | 28 | 2800 |