如何使用SQL Server 2005中的和和或运算符联接到表 [英] How to join to tables using and, or opeators in sql server 2005

查看:76
本文介绍了如何使用SQL Server 2005中的和和或运算符联接到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,
请找到下面的查询和输出.实际上,我们正在联接Sheet1和Sheet2表,并且如果在Sheet1中stpe是'rework'并且在sheet2中step是'External',则结果将显示如下输出.但是,如果有任何一个步骤可用,如此查询中的sheet1.step ="Rework"或Sheet2.Step ="External",如何获取结果.
我想要如下所示的结果
1)

Dear All,
Please find the below query and output.accatuly we are Join the Sheet1 and Sheet2 tables and the result will be displayed as shown below output if stpe is ‘rework’ in Sheet1 and step is ‘External’ in sheet2 . But how to get the result if any one step is available like sheet1.step ="Rework" or Sheet2.Step="External" in this query.
I want the result as shown below
1)

map                              sheet1.step      Sheet2.step
21-147-00021-0000_20110608       Rework



2)


OR
2)

map                              sheet1.step      Sheet2.step
21-147-00021-0000_20110608                            External


查询


Query

SELECT Sheet1.map, Sheet1.step, Sheet2.step 
FROM Sheet1 
INNER JOIN Sheet2 ON (Sheet1.mapType = Sheet2.mapType) AND 
                     (Sheet1.map = Sheet2.map) AND 
                     (Sheet1.batch = Sheet2.batch) AND 
                     (Sheet1.area = Sheet2.area) 
WHERE (((Sheet1.area)="Completions") AND 
       ((Sheet1.batch)="Michigan") AND 
       ((Sheet1.step)="Rework") AND 
       ((Sheet2.Step)="External")) 
GROUP BY Sheet1.map, Sheet1.step, Sheet2.step;

输出

map	                        Sheet1.step	Sheet2.step
21-147-00021-0000_20110608	Rework	        External




[edit]缩进,代码块.重新格式化的查询将变得可读-OriginalGriff [/edit]




[edit]Indentation, code blocks. Reformatted query to be readable - OriginalGriff[/edit]

推荐答案

SELECT Sheet1.map,Sheet1.step,Sheet2.step
从Sheet1
内联Sheet2 ON(Sheet1.mapType = Sheet2.mapType)AND
(Sheet1.map = Sheet2.map)AND
(Sheet1.batch = Sheet2.batch)AND
(Sheet1.area = Sheet2.area)
在哪里((((Sheet1.area)="Completions")AND
((Sheet1.batch)=密歇根州")AND
((Sheet1.step)=返工")AND
(((Sheet2.Step)=外部"))或((((Sheet1.area)=完成")AND
((Sheet1.batch)=密歇根州")AND
((Sheet1.step)=返工")或
((Sheet2.Step)=外部"))
GROUP BY Sheet1.map,Sheet1.step,Sheet2.step




希望这会对您有所帮助
SELECT Sheet1.map, Sheet1.step, Sheet2.step
FROM Sheet1
INNER JOIN Sheet2 ON (Sheet1.mapType = Sheet2.mapType) AND
(Sheet1.map = Sheet2.map) AND
(Sheet1.batch = Sheet2.batch) AND
(Sheet1.area = Sheet2.area)
WHERE (((Sheet1.area)="Completions") AND
((Sheet1.batch)="Michigan") AND
((Sheet1.step)="Rework") AND
((Sheet2.Step)="External")) OR (((Sheet1.area)="Completions") AND
((Sheet1.batch)="Michigan") AND
((Sheet1.step)="Rework") OR
((Sheet2.Step)="External"))
GROUP BY Sheet1.map, Sheet1.step, Sheet2.step




Hope this will help you out


这篇关于如何使用SQL Server 2005中的和和或运算符联接到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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