欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用手册_使用

程序员文章站 2022-07-02 21:23:50
...

使用手册

使用WHERE SQL子句 (Using the WHERE SQL clause)

WHERE clause is used to specify/apply any condition while retrieving, updating or deleting data from a table. This clause is used mostly with SELECT, UPDATE and DELETEquery.

WHERE子句用于在从表中检索,更新或删除数据时指定/应用任何条件。 此子句主要用于SELECTUPDATEDELETE查询。

When we specify a condition using the WHERE clause then the query executes only for those records for which the condition specified by the WHERE clause is true.

当我们使用WHERE子句指定条件时,查询仅对WHERE子句指定的条件为true的那些记录执行。

WHERE子句的语法 (Syntax for WHERE clause)

Here is how you can use the WHERE clause with a DELETE statement, or any other statement,

这是将WHERE子句与DELETE语句或任何其他语句一起使用的方法,

DELETE FROM table_name WHERE [condition];

The WHERE clause is used at the end of any SQL query, to specify a condition for execution.

WHERE子句用于任何SQL查询的末尾,以指定执行条件。

实例时间 (Time for an Example)

Consider a table student,

考虑一个餐桌学生

s_id name age address
101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai
s_id 名称 年龄 地址
101 亚当 15 钦奈
102 亚历克斯 18 新德里
103 阿比 17 邦洛尔
104 安奇 22 孟买

Now we will use the SELECT statement to display data of the table, based on a condition, which we will add to our SELECT query using WHERE clause.

现在,我们将根据条件使用SELECT语句显示表的数据,并使用WHERE子句将其添加到SELECT查询中。

Let's write a simple SQL query to display the record for student with s_id as 101.

让我们编写一个简单SQL查询来显示s_id为101的学生的记录。

SELECT s_id, 
    name, 
    age, 
    address 
    FROM student WHERE s_id = 101;

Following will be the result of the above query.

以下是上述查询的结果。

s_id name age address
101 Adam 15 Noida
s_id 名称 年龄 地址
101 亚当 15 野田

在文本字段上应用条件 (Applying condition on Text Fields)

In the above example we have applied a condition to an integer value field, but what if we want to apply the condition on name field. In that case we must enclose the value in single quote ' '. Some databases even accept double quotes, but single quotes is accepted by all.

在上面的示例中,我们将条件应用于整数值字段,但是如果我们想将条件应用于name字段怎么办。 在这种情况下,必须将值括在单引号' ' 。 有些数据库甚至接受双引号,但所有人都接受单引号。

SELECT s_id, 
    name, 
    age, 
    address 
    FROM student WHERE name = 'Adam';

Following will be the result of the above query.

以下是上述查询的结果。

s_id name age address
101 Adam 15 Noida
s_id 名称 年龄 地址
101 亚当 15 野田

WHERE子句条件的运算符 (Operators for WHERE clause condition)

Following is a list of operators that can be used while specifying the WHERE clause condition.

以下是在指定WHERE子句条件时可以使用的运算符的列表。

Operator Description
= Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or Equal to
>= Greate than or Equal to
BETWEEN Between a specified range of values
LIKE This is used to search for a pattern in value.
IN In a given set of values
操作员 描述
= 等于
!= 不等于
< 少于
> 比...更棒
<= 小于或等于
>= 大于或等于
BETWEEN 在指定的值范围之间
LIKE 这用于搜索值的模式。
IN 在给定的一组值中

翻译自: https://www.studytonight.com/dbms/where-clause.php

使用手册