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

问题描述



嗨组

我有一个相当特殊的问题,而且我真的不知道怎么解决这个问题

SQL语句:


给定一个视图(v),结果是:


ID X

- ---------------

1 a

1 b

2 a

2 c

3 a


我想用以下内容查询视图:

SELECT ID FROM v WHERE(X =''''和X =''b'')会导致:


ID

---- -------------

1


或其他情况:

SELECT ID FROM v WHERE(X =''''或X =''c'')

会给:


ID

-----------------

1

2

3


怎么办呢?


TIA

bernhard


-


删除ni xspam回复

解决方案




SELECT ID FROM v WHERE X =''a''

UNION

SELECT ID FROM v WHERE X =''b''


-

Erland Sommarskog,SQL Server MVP,


SQL Server SP3联机丛书






非常感谢您的回答。离开你的建议我已经来了以下解决方案(这似乎适用于我的目的):


SELECT id

FROM v

WHERE x =''''和EXISTS

(SELECT id FROM v WHERE x =''b'')

GROUP BY id


这可以轻松采用''或''案例:


SELECT id

FROM v

WHERE x =''''或EX EXTS

(SELECT id FROM v WHERE x =''b'')

GROUP BY id


您怎么看?


bernhard


-


删除nixspam回复



Hi group
I have a rather peculiar question, and I really don''t know how to solve
this within an SQL statement:

Given a view (v), that results in:

IDX
-----------------
1a
1b
2a
2c
3a

I''d like to query the view with something like:
SELECT ID FROM v WHERE (X=''a'' AND X=''b'') which would result in:

ID
-----------------
1

or in another case:
SELECT ID FROM v WHERE (X=''a'' OR X=''c'')
would give:

ID
-----------------
1
2
3

how can this be done?

TIA
bernhard

--
www.daszeichen.ch
remove nixspam to reply

解决方案



SELECT ID FROM v WHERE X = ''a''
UNION
SELECT ID FROM v WHERE X = ''b''

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp





thanks a lot for your answer. Departing from your suggestion I have come
to the following solution (which seem to work for my purposes):

SELECT id
FROM v
WHERE x=''a'' AND EXISTS
(SELECT id FROM v WHERE x=''b'')
GROUP BY id

This can easily adopted to the ''or'' case:

SELECT id
FROM v
WHERE x=''a'' OR EXISTS
(SELECT id FROM v WHERE x=''b'')
GROUP BY id

What do you think?

bernhard

--
www.daszeichen.ch
remove nixspam to reply


这篇关于AND在单列中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:07