sql exists

Hey, folks! Hope you all are doing well. In this article, we will be focusing on SQL Exists Operator in detail.

嘿伙计! 希望大家一切都好。 在本文中,我们将重点关注SQL Exists运算符



存在SQL的工作 (Working of SQL Exists)

SQL Exists is an SQL Operator works with a subquery and enables us to check for the presence of particular records in that subquery. Further, it checks whether the subquery returns some records from the table depending upon the condition.

SQL Exists是一个SQL Operator与子查询一起使用,使我们能够检查该子查询中特定记录的存在。 此外,它根据条件检查子查询是否从表中返回一些记录。

Let us have a look at the below example to the operator.

让我们来看下面的示例给操作员。

Consider an Online Portal where the owner decides to offer a discount of $20 to every customer whose bills are higher than $350.

考虑一个在线门户,所有者决定为账单高于350美元的每个客户提供20美元的折扣。

For the same, the developer team at the backend would segregate the database and use the Exists operator to check for the presence of such condition in the various tables.

同样,后端的开发人员团队将隔离数据库,并使用Exists运算符检查各种表中是否存在这种情况。

Thus, SQL Exists executes a certain query, upon the presence of data returned by the subquery.

因此,SQL Exists在子查询返回的数据存在时执行特定查询。

Let us now understand the Syntax of Exists Operator in detail.

现在让我们详细了解“存在运算符”的语法。



存在运算符的语法 (Syntax of the Exists Operator)

The Exists Operator works well with SQL Update, Delete, Select and Insert query.

Exists运算符与SQL Update,Delete,Select和Insert查询配合使用很好。


SQL Update/Insert/Delete/Select query
WHERE EXISTS
(sub-query);

EXISTS evaluates to TRUE, if and only if the subquery gets executed and returns the specified records.

当且仅当子查询被执行并返回指定的记录时,EXISTS的计算结果才为TRUE。

The Sub query can be used to query another database too if the column values are comparable.

如果列值可比,则Sub查询也可用于查询另一个数据库。

Having understood the working and structure of Exists Operator, let us now focus on the implementation of the same in the upcoming section.

了解了Exists的工作原理和结构后,现在让我们在接下来的部分中重点介绍相同操作的实现。



通过示例实现SQL EXISTS (Implementing SQL EXISTS through examples)

Let us first create the table in SQL that we would be using in the below examples. Using some random names of places here for demonstration.

让我们首先在下面的示例中使用SQL创建表。 在此处使用一些随机的地点名称进行演示。


create table Info(id integer, Cost integer, city varchar(20));
insert into Info(id, Cost, city) values(1, 100, Pune);
insert into Info(id, Cost, city) values(2, 50, Satara);
insert into Info(id, Cost, city) values(3, 65, Pune);
insert into Info(id, Cost, city) values(4, 97, Mumbai);
insert into Info(id, Cost, city) values(5, 12, USA);

Output:

输出:

id Cost city
1 100 Pune
2 50 Satara
3 65 Pune
4 97 Mumbai
5 12 USA
ID 成本
1个 100 浦那
2 50 萨塔拉
3 65 浦那
4 97 孟买
5 12 美国

create table Supply(id integer, Cost integer);
insert into Supply(id, Cost) values(1, 100);
insert into Supply(id, Cost) values(2, 200);
insert into Supply(id, Cost) values(3, 300);
insert into Supply(id, Cost) values(4, 400);
insert into Supply(id, Cost) values(5, 12);

Output:

输出:

SQL Exists–Supply TableSQL存在-供应表

As mentioned above, we have created the tables — ‘Info’ and ‘Supply’ using SQL Create table query and have added data to it using SQL Insert command.

如上所述,我们已经使用SQL Create表查询创建了表-“ Info”和“ Supply”,并使用SQL Insert命令向其中添加了数据。



SQL存在选择查询 (SQL Exists with Select query)

In the below example, we have used SQL Select query along with the Exists Operator. The below query selects and displays row values of ‘id’ and ‘City’ only if ‘Cost’ has data values greater than 50.

在下面的示例中,我们将SQL Select查询与Exists运算符一起使用。 仅当“成本”的数据值大于50时,以下查询才选择并显示“ id”和“城市”的行值。


SELECT id,CityFROM InfoWHERE EXISTS(SELECT CostFROM InfoWHERE Cost>50);

Output:

输出:

SQL Exists–SELECT StatementSQL存在– SELECT语句


SQL存在删除查询 (SQL Exists with Delete query)

Now, we have implemented SQL Delete query with SQL Exists operator to perform the operation as follows–

现在,我们使用SQL Exists运算符实现了SQL Delete查询 ,以执行以下操作–

The below query would delete a data row from the table ‘Info’ if and only if, the column ‘id’ from both the tables(Info and Supply) is the same and there exists a value for ‘Cost’ as 12 in the Supply table.

当且仅当以下两个表(“信息”和“供应”)中的“ id”列相同且“供应”中存在“成本”的值为12时,以下查询才会从“信息”表中删除数据行表。


DELETE
FROM Info
WHERE EXISTS (SELECT *FROM SupplyWHERE Info.id = Supply.idAND Supply.Cost = 12);

Output:

输出:

SQL Exists–DELETE StatementSQL存在–删除语句


SQL存在更新查询 (SQL Exists with Update query)

In this example, we have used the SQL Update query along with SQL Exists Operator. All the data values of the column ‘Cost’ would be set to 20 if and only if the value for column ‘id’ of both the table happens to be equal.

在此示例中,我们将SQL Update查询与SQL Exists运算符一起使用。 当且仅当两个表的“ id”列的值恰好相等时,“ Cost”列的所有数据值都将设置为20。


Update Info
set Cost = 20
WHERE EXISTS (SELECT *FROM SupplyWHERE Info.id = Supply.id);

Output:

输出:

SQL Exists–UPDATE StatementSQL存在-UPDATE语句


结论 (Conclusion)

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any doubt.

至此,我们到了本主题的结尾。 如果您有任何疑问,请在下面发表评论。

For more such posts related to SQL, please do visit SQL JournalDev.

有关与SQL有关的更多此类帖子,请访问SQL JournalDev 。



参考资料 (References)

  • SQL Exists Operator — DocumentationSQL Exists运算符-文档

翻译自: https://www.journaldev.com/42152/sql-exists-operator

sql exists

sql exists_SQL Exists运算符–终极指南相关推荐

  1. t–sql pl–sql_SQL存储过程–终极指南

    t–sql pl–sql Hey, folks! In this article, we will be focusing on SQL Stored Procedures. 嘿伙计! 在本文中,我们 ...

  2. SQL Exists运算符

    EXISTS 运算符是一个逻辑运算符,用于检查子查询是否返回任何行. 如果子查询返回一行或多行,则 EXISTS 运算符返回 TRUE . 以下是SQL Server EXISTS 运算符的语法: E ...

  3. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》

    本周为大家送出的书是<Oracle 12 c PL(SQL)程序设计终极指南>,此书由机械工业出版社出版, 孙风栋,王澜,郭晓惠 著. 内容简介: <Oracle 12c PL/SQ ...

  4. oracle 12c sql图形化,Oracle 12c PL/SQL程序设计终极指南

    Oracle 12c PL/SQL程序设计终极指南 作者:孙风栋;王澜;郭晓惠 出版日期:2015年06月 文件大小:11.73M 支持设备: ¥60.00在线试读 适用客户端: 言商书局 iPad/ ...

  5. 如何编写更好的SQL查询:终极指南-第二部分

    上一篇文章中,我们学习了 SQL 查询是如何执行的以及在编写 SQL 查询语句时需要注意的地方. 下面,我进一步学习查询方法以及查询优化. 基于集合和程序的方法进行查询 反向模型中隐含的事实是,建立查 ...

  6. T-SQL命令性能比较– NOT IN与SQL NOT EXISTS与SQL LEFT JOIN与SQL EXCEPT

    This articles gives you a performance comparison for NOT IN, SQL Not Exists, SQL LEFT JOIN and SQL E ...

  7. 【SQL Server】数据库开发指南(五)T-SQL 高级查询综合应用与实战

    本系列博文还在更新中,收录在专栏:#MS-SQL Server 专栏中. 本系列文章列表如下: [SQL Server] Linux 运维下对 SQL Server 进行安装.升级.回滚.卸载操作 [ ...

  8. SQL之exists的使用

    1.exists介绍 EXISTS (子查询) :如果子查询包含任何行,则EXISTS运算符返回true. 否则它返回false. 作用:用exists代替in是SQL性能优化的一个手段,使用exis ...

  9. SQL中EXISTS理解使用

    SQL中EXISTS的理解使用 关联子查询 EXISTS理解使用 关联子查询 在讲述EXISTS用法之前,先讲述一下关联子查询: 关联子查询:是指在内查询中需要借助于外查询,而外查询离不开内查询的执行 ...

最新文章

  1. 三十八、页面分配策略
  2. 【机器学习入门到精通系列】蒙特卡罗方法简介和代码演示
  3. ai如何旋转画布_Ai绘制科技感晶格球体!
  4. Vue.js示例:GitHub提交(watch数据,created钩子,filters过滤); 网格组件(功能:1.检索,2排序);...
  5. 【译】如何使用索引视图和一个只有2行的表限制业务规则
  6. 大牛深入浅出讲解C语言#define宏定义应用及使用方法
  7. 树的存储结构以及实现代码
  8. 最大公约数和最小公倍数 模板
  9. UESTC_秋实大哥与花 2015 UESTC Training for Data StructuresProblem B
  10. Python动态导入模块、类
  11. webx学习(二)——Webx Framework
  12. CentOS网络配置与重启方法
  13. firstchild.data与childNodes[0].nodeValue意思(转)
  14. LNMP环境搭建笔记
  15. 世界主要国家地区英文名称,缩写代码
  16. 腾讯云短信申请与使用
  17. 联想电脑EasyCamera无法打开摄像头
  18. 【Tableau server7.0_私人版本】Tableau Server Certified Associate(Tableau CA) 考前预备
  19. 二元隐函数求二阶偏导_多元函数隐函数微分 二阶偏导的求法
  20. 定制小狼豪(五笔+拼音)输入法

热门文章

  1. iOS 4中禁止程序退出后保留在后台的方法
  2. [转载] python 函数参数类型检查
  3. 前端组件化思想与实践
  4. pycharm——常用快捷键操作
  5. 设计模式之笔记--建造者模式(Builder)
  6. Oracle行转列、列转行的Sql语句总结
  7. linux centos 系统php支持jpeg的安装方法
  8. POJ 3761 Bubble Sort(乘方取模)
  9. 解释Spring中IOC, DI, AOP
  10. HDU 3394 Railway(点双连通分量)