返回 SQL Server 2000 上 IN 子句的所有值
有没有办法从 IN
子句中检索所有数据?
假设我的表有(ID,名称):
0 Banana
1个芒果
2个木瓜
3 Lemon
和我的查询:
SELECT * FROM Fruits WHERE Name IN (Banana,Mango,Orange)
我希望返回“Orange”,且 ID 为空(因为没有寄存器)。如何做到这一点?
Is there a way to retrieve all the data from an IN
clause?
Let's assume my table got (ID,Name):
0 Banana
1 Mango
2 Papaya
3 Lemon
and my query:
SELECT * FROM Fruits WHERE Name IN (Banana,Mango,Orange)
I Want 'Orange' to return, with an empty ID (since there's no register). How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能为此使用
IN
子句。您需要将目标水果放入可以外部连接的表中。或选项 2(只要您的列表 <= 8000 个字符)。创建一个 UDF 像这里的那样(但使用
varchar( 8000)
而不是varchar(max)
)。然后按如下方式使用它。You can't use the
IN
clause for this. You would need to get the target fruits into a table that you can outer join against.Or Option 2 (as long as your list is <= 8000 characters). Create a UDF like the one here (but using
varchar(8000)
instead ofvarchar(max)
). Then use it as follows.按名称右连接,但您需要一个包含名称的单独表。
Right join by name, but you need a separate table with names.
如果你这样做一次,我写了一个程序,它简化了创建“IN”子句,在这里: InClauseCreator 。但是,如果您经常这样做,那么最好将数据导入到临时表中,然后进行左连接,这与 Martin Smith 的建议接近。
If you are doing it once, I wrote a program, it simplifies creating the "IN" clause, here: InClauseCreator. If, however, you are doing it regularly, then you'd be better off importing data into a temp table and then doing a left join, close to what Martin Smith suggested.