sql - and - or
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:
USE mydatabase; SELECT *
FROM orders
WHERE customer = 'Tizag'
AND day_of_order = '08/01/08'
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:
USE mydatabase; SELECT *
FROM orders
WHERE product = 'Pen'
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:
USE mydatabase; SELECT *
FROM orders
WHERE (quantity > 2 AND customer = 'Tizag')
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的更多相关文章
- 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目
最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...
- SQL Server 大数据搬迁之文件组备份还原实战
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...
- Sql Server系列:分区表操作
1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- EntityFramework Core Raw SQL
前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
- 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- SQL Server on Linux 理由浅析
SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...
随机推荐
- 使用AutoMapper
一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...
- mysql---字符集详解
常用的字符集包括ASCII ,GB2312 , GBK , UTF-8 ,Unicode 首先要知道 ASCII编码: 用一个字节来标识0-9的数字.大小写字母.及一些标点和不可见字符.1个字节8位, ...
- php使用phpmailer发送邮件
本人新手,由于要做邮件发送验证码,所以找到和搜集到这些,本人亲测完全可以用 这是163邮箱的 因为不是企业邮箱填写的账号是163的账号,但是密码是授权码 授权码的获取方式为:
- oracle11g 表或视图连接时有可能发生的问题
---------背景--------- oracle11g 有2个视图,都有一个id字段,且id字段的值相同 例如:id都有 A01 ,A02 ,A03 --------问题--------- 把2 ...
- (转载)Delphi TStringList的用法
Delphi TStringList的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. TStringList 常用方法与属性: var List: TStringL ...
- 注意WPF中绑定使用的是引用类型
绑定(Binding)是WPF提供的一个非常方便的特性,它可以方便的实现一个WPF的MVVM结构. 既可以实现数据驱动UI变化,也可以做到End-user在UI上的修改实现的反映到数据上. 但是有一点 ...
- java学习:用反射构造bean
先贴一些反射的基本知识:-------------------------------------------------------------------- 一.什么是反射:反射的概念是由Smit ...
- Java 开发者不容错过的 12 种高效工具
Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高效编程工具.所以,以下总结了一系列工具列表,其中包含了大多数开发人员已经使用.正在使用或 ...
- configure文件中判断某函数或库是否存在的一个方法
echo " #include<stdio.h> #include<openssl/ssl.h> int main() { return 0; } " &g ...
- C语言关键字 - 铁布衫:const 转载
const 描述:相传C世界中出现了一件极品装备const,它能的出现,让天下所有的刺客,黑客都失业了,在它的保护下,所有的变量都可以完好无损. 作用:const是constant的简写,表示海枯石栏 ...