When we have to UPDATE, DELETE or SELECT a particular set of data we use SQL WHERE clause. The WHERE clause is an integral part of SQL as it provides the feature to add conditions to a query.

当我们必须UPDATE , DELETE或SELECT特定数据集时,我们使用SQL WHERE子句。 WHERE子句是SQL的组成部分,因为它提供了向查询添加条件的功能。

SQL Where子句 (SQL Where Clause)

SQL where clause is one of the most widely used SQL keyword because it’s part of most of the queries. Let’s see the general syntax of where clause in sql query.

SQL where子句是最广泛使用的SQL关键字之一,因为它是大多数查询的一部分。 让我们看看sql查询中where子句的一般语法。

SELECT column(s) FROM table_name WHERE condition;

In the syntax above the column data is retrieved based on the condition that is specified as part of WHERE condition.

在上面的语法中,基于指定为WHERE条件一部分的条件检索列数据。

Similarly we can use it with Update and Delete query as follows:

同样,我们可以将其与Update和Delete查询一起使用,如下所示:

update Employee set name = 'David' where id=1;Delete from Employee where name = 'John';

SQL查询位置示例 (SQL Query Where Example)

Let’s try to understand the SQL WHERE command through some example. We have a sample table with below structure and data.

让我们尝试通过一些示例来了解SQL WHERE命令。 我们有一个示例表,其中包含以下结构和数据。

.tg {border-collapse:collapse;border-spacing:0;border-color:#aaa;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#fff;background-color:#f38630;} .tg .tg-j2zy{background-color:#FCFBE3;vertical-align:top} .tg .tg-yw4l{vertical-align:top} .tg .tg-vvaf{background-color:#FCFBE3;font-weight:bold;vertical-align:top} .tg .tg-9hbo{font-weight:bold;vertical-align:top}.tg {border-collapse:collapse;border-spacing:0;border-color:#aaa;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#fff;background-color:#f38630;} .tg .tg-j2zy{background-color:#FCFBE3;vertical-align:top} .tg .tg-yw4l{vertical-align:top} .tg .tg-vvaf{background-color:#FCFBE3;font-weight:bold;vertical-align:top} .tg .tg-9hbo{font-weight:bold;vertical-align:top}

CustomerId CustomerName CustomerAge CustomerGender
1 John 31 M
2 Amit 25 M
3 Annie 35 F
顾客ID 顾客姓名 客户年龄 客户性别
1个 约翰 31 中号
2 阿米特 25 中号
3 安妮 35 F

Now we want to select all the customer names who are Female. Below is the SQL query with where clause to get this data.

现在,我们要选择所有女性客户名称。 以下是带有where子句SQL查询,用于获取此数据。

SELECT CutomerName FROM Customer WHERE CustomerGender = 'F';

SQL Where子句运算符 (SQL Where Clause Operators)

Since where clause is to add conditions to the query, there are many operators that we can use with it. Let’s look into commonly used operators with sql query where clause.

由于where子句用于向查询添加条件,因此我们可以使用许多运算符。 我们来看一下带有sql query where子句的常用运算符。

.tg {border-collapse:collapse;border-spacing:0;border-color:#aaa;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#fff;background-color:#f38630;} .tg .tg-j2zy{background-color:#FCFBE3;vertical-align:top} .tg .tg-z2zr{background-color:#FCFBE3} .tg .tg-yw4l{vertical-align:top}.tg {border-collapse:collapse;border-spacing:0;border-color:#aaa;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#fff;background-color:#f38630;} .tg .tg-j2zy{background-color:#FCFBE3;vertical-align:top} .tg .tg-z2zr{background-color:#FCFBE3} .tg .tg-yw4l{vertical-align:top}

Operator Description Example
= Equal to SELECT CustomerNameFROM Customer WHERE CustomerGender = ‘F’;
!= , <> Not equal to SELECT CustomerName FROM Customer WHERECustomerGender != ‘M’;
> Greater than SELECT CustomerNameFROM Customer WHERE CustomerAge > 32;
< Less than SELECT CustomerName FROM Customer WHERECustomerAge < 30;
>= Greater than or equalto SELECT CustomerNameFROM Customer WHERE CustomerAge >= 35;
<= Less than or equal to SELECT CustomerName FROM Customer WHERECustomerAge <= 30;
IN Value is from thespecified list of values SELECT CustomerNameFROM Customer WHERE CustomerGender IN (‘M’, ‘F’);
BETWEEN Between the specified range SELECT CustomerName FROM Customer WHERECustomerAge BETWEEN (25,30);
LIKE Search for pattern SELECT CustomerNameFROM Customer WHERE CustomerName like ‘Ann%’;
操作员 描述
= 等于 SELECT CustomerNameFROM Customer WHERE CustomerGender ='F';
!=,<> 不等于 从客户WHERECustomerGender中选择客户名!='M';
> 比...更棒 选择CustomerNameFROM Customer WHERE CustomerAge> 32;
< 少于 从客户WHERECustomerAge <30中选择客户名称;
> = 大于或等于 选择CustomerNameFROM Customer WHERE CustomerAge> = 35;
<= 小于或等于 从客户WHERECustomerAge <= 30中选择客户名称;
值来自指定的值列表 选择CustomerNameFROM客户所在的CustomerGender输入(“ M”,“ F”);
之间 在指定范围之间 从(25,30)之间的客户WHERECustomerAge中选择客户名称;
喜欢 搜索图案 SELECT CustomerNameFROM Customer WHERE CustomerName之类的“ Ann%”;

Numeric and text are used as part of the WHERE clause values in different ways. To use Numeric no quotes should be used. For using a text value single quotes should be used.

数字和文本以不同方式用作WHERE子句值的一部分。 要使用数字,不应该使用引号。 对于使用文本值,应使用单引号。

That’s all for SQL where clause. Where clause in SQL query is used a lot, so you should understand it properly.

这就是SQL where子句的全部内容。 SQL查询中的where子句经常使用,因此您应该正确理解它。

翻译自: https://www.journaldev.com/18411/sql-where-clause

SQL Where子句相关推荐

  1. SQL函数---SQL HAVING 子句

    SQL HAVING 子句 HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. SQL HAVING 语法 SELECT column_nam ...

  2. SQL基础---SQL WHERE 子句

    SQL WHERE 子句 WHERE 子句用于规定选择的标准. WHERE 子句 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句. 语法 SELECT 列名称 FROM ...

  3. Mysql: SQL JOIN 子句详解

    SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段. 最常见的 JOIN 类型:SQL INNER JOIN(简单的 JOIN).SQL LEFT JOIN.SQL  ...

  4. sql limit 子句_Java 8流中的常见SQL子句及其等效项

    sql limit 子句 功能编程允许使用通用语言进行准声明性编程 . 通过使用功能强大的流畅API(例如Java 8的Stream API )或jOOλ的顺序Stream扩展Seq或更复杂的库(例如 ...

  5. sql limit子句_SQL子句解释的位置:之间,之间,类似和其他示例

    sql limit子句 什么是SQL Where子句? (What is a SQL Where Clause?) WHERE子句(和/或IN , BETWEEN和LIKE ) (The WHERE ...

  6. sql limit 子句_SQL Server中的FOR XML PATH子句

    sql limit 子句 As SQL professionals, we often have to deal with XML data in our databases. This articl ...

  7. sql limit 子句_SQL Order by子句概述

    sql limit 子句 In this article, we'll walk-through the concept of the SQL Order by clause and understa ...

  8. sql limit 子句_SQL Join子句介绍和概述

    sql limit 子句 The SQL Join clause is one of the major components of the Select statement, which is us ...

  9. sql limit 子句_SQL按子句概述和示例

    sql limit 子句 This article will cover the SQL ORDER BY clause including syntax, usage scenarios to so ...

  10. sql limit 子句_具有并行性SQL Server TOP子句性能问题

    sql limit 子句 TOP操作员基础 ( Basics of TOP Operator ) The TOP keyword in SQL Server is a non-ANSI standar ...

最新文章

  1. 全网仅此一篇:工业级压力传感器设计及实现(华大半导体HC32L136)
  2. 【电子基础】模拟电路问答
  3. sshd系统自带启动脚本详解
  4. php cannot find libz,brew安装php70出现configure: error: Cannot find libz 错误解决方法
  5. wxWidgets:wxFileDialog类用法
  6. 阿里云免费申请免费SSL证书
  7. CloudFoundry和AWS上应用监听的端口号
  8. 不要手贱自己去通过Javascript画html界面
  9. 前端学习(1815):前端调试之css flex 练习1
  10. oopc——1.抽象
  11. 在windows7家庭版安装软件时可能存在的问题
  12. icmp基于tcp还是udp_imi -- 基于PHP协程应用开发框架,支持WebSocket,TCP,UDP应用开发...
  13. 加速nginx: 开启gzip
  14. 图解CSS3----4-结构伪类选择器
  15. 超级计算器 android,超级计算器解方程
  16. Redis主从复制-Replication
  17. js实现json格式化,以及json校验工具的简单实现
  18. 开机要按F1的解决方法
  19. c#web网页连接数据库产品出库入库数据更新
  20. 电脑休眠唤醒后无法显示WIFI列表

热门文章

  1. ios 将随意对象存进数据库
  2. radio select的 option使用
  3. [转载] python 等号 重载_python运算符重载2
  4. 带你玩转JavaWeb开发之四 -如何用JS做登录注册页面校验
  5. 局域网电脑Sql2008 R2无法连接到localhost 解决方案
  6. 如何实现TextBox与DropDownList的级联
  7. C++之---友元函数
  8. CV学习笔记-数字图像概述
  9. stl非变易算法(一)
  10. 路由器上的usb接口有什么用_路由器上面的USB接口有什么用途,你都知道吗?