sql - and

SQL AND links together two or more conditional statements for increased filtering when running SQL commands. AND helps the developer query for very specific records while answering questions like, "I want to view all orders made by a certain customer AND made on a special date." There is no limit to the number of AND/OR conditions that can be applied to a query utilizing the WHERE clause. This makes it possible for the developer to be as precise as needed when querying for results.

 


SQL And Code:

  1. USE mydatabase;
  2.  
  3. SELECT *
  4. FROM orders
  5. WHERE customer = 'Tizag'
  6. AND day_of_order = '08/01/08'
  7. AND product = 'Pen';

SQL Results:

id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4

This example illustrates how SQL AND combines multiple conditional statements (3 total now) into a single condition with multiple circumstances (filters). Each filter removes rows from the result set that doesn't meet the condition.

sql - or

SQL OR also applies logic to help filter results. The difference is that instead of linking together conditional statements, an OR condition just asks SQL to look for 2 separate conditions within the same query and return any records/rows matching either of the conditions.

SQL Or Code:

  1. USE mydatabase;
  2.  
  3. SELECT *
  4. FROM orders
  5. WHERE product = 'Pen'
  6. OR product = '19" LCD Screen';

SQL Results:

id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4
4 Gerald Garner 2008-08-15 00:00:00.000 19" LCD Screen 3
5 Tizag 2008-07-25 00:00:00.000 19" LCD Screen 3

The first record returned matches the first condition since the product = 'Pen'. The two records after that match the other condition; the product in each of those orders is the '19" LCD Screen'. This type of logic allows the developer to filter results based on one or more conditions.

SQL AND allows the developer to query for more specific data by linking together conditional statements. On the other end of the spectrum, SQL ORcreates a new, independent condition not linked to any other conditional statement.

sql - and / or combination

Conditional statements can be grouped together using parentheses (). Doing so links together conditions and provides robust solutions for data querying.

SQL And/Or Code:

  1. USE mydatabase;
  2.  
  3. SELECT *
  4. FROM orders
  5. WHERE (quantity > 2 AND customer = 'Tizag')
  6. OR (quantity > 0 AND customer = 'Gerald Garner')

By encapsulating the first two conditions (quantity > 2 AND customer = 'Tizag') SQL now treats this as a single condition, and the same is true for the next line. These two conditions have been linked together with the OR operator, creating very unique behavior.

SQL is now looking for rows where the customer is Tizag AND the quantity is more than 2, and ALSO looking for rows where the customer is Gerald GarnerAND the quantity is greater than 0. All rows meeting either condition will be returned as demonstrated in our results below.

SQL Results:

id customer day_of_order product quantity
1 Tizag 2008-08-01 00:00:00.000 Pen 4
4 Gerald Garner 2008-08-15 00:00:00.000 19" LCD Screen 3
5 Tizag 2008-07-25 00:00:00.000 19" LCD Screen 3

sql - and - or的更多相关文章

  1. 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目

    最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...

  2. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  3. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  4. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  5. EntityFramework Core Raw SQL

    前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...

  6. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  7. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  8. 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...

  9. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  10. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

随机推荐

  1. Linux中的简单命令

    history:打印你输过的命令      1.用户在shell中输入的命令会自动保存到内存缓冲区      2.在退出shell的时候,内存中的数据会刷新到磁盘文件:~/.bash_history ...

  2. 最大类间方差法(Otsu)

    由Otsu(大津展之)于1978年提出的最大类间方差法,是引起较多关注的一种阈值选取方法.它是在判决分析或最小二乘原理的基础上推导出来的. 参考文献: [1] Otsu N. A threshold ...

  3. 出力csv

    public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path) { if (!Sy ...

  4. 还原数据库备份文件时,关于“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个。必须提供所有成员”的处理方式

    好久没写博客了,最近在做毕设的权限管理模块,今天在还原数据库文件时,遇到了“System.Data.SqlClient.SqlError:媒体集有2个媒体簇,但只提供了1个.必须提供所有成员”这个错误 ...

  5. python学习笔记——列表生成式与生成器

    1.列表生成式(List Comprehensions) python中,列表生成式是用来创建列表的,相较于用循环实现更为简洁.举个例子,生成[1*1, 2*2, ... , 10*10],循环用三行 ...

  6. windows 系统下,小数据量Oracle用户物理备份

    环境:windows Server 2003 oracle 10g,系统间备份 目标系统创建共享文件,原系统挂载共享目录 写批处理脚本,用任务计划定时调用 Rem * 由于系统实时性要求不是很高,数据 ...

  7. homework-Agile Software Development

    对敏捷开发的一些思考 这周的作业是对敏捷开发的相关阅读和思考. 在阅读的过程中,可以看到作者是一位具有丰富编程经验的大师.在开发的经历中,作者经历了极限编程等开发过程,但是在作者的多年经验中,作者还是 ...

  8. Junit4.12、Hamcrest1.3、Eclemma的安装和使用

    1. Junit4.12和Hamcrest1.3的安装过程 步骤: 网上下载Junit和Hamcrest包文件,保存在本地. 新建Java项目命名为Triangle,在Eclipse菜单栏选择项目(P ...

  9. python 常用模块(转载)

    转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...

  10. IIS Handler and Module探索

    Create Handler & Module Run the Visual Studio Create a Class Library “HMHandler” --> Change t ...