问题描述
我有一个数据库.在SQL中进行减号运算后,我得到了一定的结果,但是我无法从中获得前1个元素.任何人都可以帮忙....
这是我的查询
i have a database. after MINUS operation in SQL i get certain result but i am unable to get top 1 element from it. can anyone pls help....
here is my query
select top 1 Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.BookId=''99''
except
select Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.StockId = Issue_Book.StockId
该查询将获取不在库存和发行书籍中的stockid.
this query gets stockid which is not in bookstock and issue book.
result is like<br />
-----------<br />
stock id<br />
173<br />
174<br />
176<br />
177<br />
186<br />
187
现在如何在上述单个查询中从上述结果中获取前1个stockid?
now how can i get the top 1 stockid from the above result, in the single above query?
推荐答案
select top 1 Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.BookId=''99''
except
select Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.StockId = Issue_Book.StockId
那将行不通-第一个SELECT只会返回表中的第一行,然后根据排除列表进行检查.如果要返回组合排除的最高结果,则需要嵌套查询:
That won''t work - the first SELECT will only return the top row in the table, which is then checked against the exclusion list. If you want to return the top result of the combined exclusion, you need to nest the queries:
SELECT TOP 1 * FROM myTable WHERE iD IN
(
SELECT iD FROM myTable WHERE iD > 5
EXCEPT
SELECT iD FROM myTable WHERE iD < 10
)
您必须根据实际需要微调它,但这就是原理.
You will have to jiggle it to match what you want to do exactly, but that''s the principle.
Select Top 1 Book_Stock.StockId from (select Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.BookId=''99''
except
select Book_Stock.StockId from Book_Stock,Issue_Book where Book_Stock.StockId = Issue_Book.StockId) AS A
这篇关于如何在SQL中进行减号运算后获得前1个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!