在单个sql查询中编写多个条件 [英] write multiple conditions in a single sql query

查看:95
本文介绍了在单个sql查询中编写多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是sql的新手.我处于一种情况,我必须在一个查询中放入三个不同的条件.如果满足以下任何条件,则应在消息记录"中显示消息框",否则将在消息记录"中显示消息框".

条件1:

I am new to sql. I am in a situation where i have to put three different conditions in a single query. If any of the below conditions retreive records MessageBox should be shown "Records Retreived" else "Nothing Retreived".

Condition 1 :

"Select Name, Age from Table1 where Class = '1st' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"#" .



条件2:



Condition 2 :

"Select Name, Age from Table1 where Class = '2nd' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"#"

.

条件3:

.

Condition 3 :

"Select Name, Age from Table1 where Class = '2nd' and Date > #"+1st_Date.ToString()+"# and Date  < #"+2nd_Date.ToString()+"#"



我的问题是如何在单个查询中编写这三个条件.希望你理解我的问题.我一直在寻找答案,但徒劳无功.我将非常感谢您.



My question is how to write these three conditions in a single query. Hope you understand my question. I am searching for the answer for very long but all in vain. I will be very thankful of yours.

推荐答案

使用OR运算符:
Use the OR operator:
Select Name, Age from Table1 where 
(Class = '1st' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"# ) OR
(Class = '2nd' and Date between #"+1st_Date.ToString() +"# and #"+2nd_Date.ToString() +"# ) OR
(Class = '2nd' and Date > #"+1st_Date.ToString()+"# and Date < #"+2nd_Date.ToString()+"#)


SELECT
(CASE WHEN
EXISTS
(
SELECT condition 1
UNION ALL
SELECT condition 2
UNION ALL
SELECT condition 3
)
THEN
'Records retreived'
ELSE
'Nothing retreived'
END)
AS MessageBoxText


这篇关于在单个sql查询中编写多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆