使用SQL,您可以在WHERE LIKE子句中使用子查询吗? [英] With SQL can you use a sub-query in a WHERE LIKE clause?
本文介绍了使用SQL,您可以在WHERE LIKE子句中使用子查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从概念上来说这听起来很奇怪,我什至不知道该如何表达,但我会尝试一下。基本上,我正在寻找一种创建实质上是WHERE IN LIKE SELECT语句的查询的方法。
I'm not even sure how to even phrase this as it sounds weird conceptually, but I'll give it a try. Basically I'm looking for a way to create a query that is essentially a WHERE IN LIKE SELECT statement.
例如,如果我想查找具有hotmail.com电子邮件地址的所有用户记录,则可以执行以下操作:
As an example, if I wanted to find all user records with a hotmail.com email address, I could do something like:
SELECT UserEmail
FROM Users
WHERE (UserEmail LIKE '%hotmail.com')
但是如果我想使用子查询作为匹配条件怎么办?像这样的东西:
But what if I wanted to use a subquery as the matching criteria? Something like this:
SELECT UserEmail
FROM Users
WHERE (UserEmail LIKE (SELECT '%'+ Domain FROM Domains))
那有可能吗?如果是这样,什么是正确的语法?
Is that even possible? If so, what's the right syntax?
推荐答案
您可以尝试
SELECT u.*
FROM Users u INNER JOIN
Domains d ON u.UserEmail LIKE '%'+ d.Domain
甚至尝试
SELECT u.*
FROM Users u
WHERE EXISTS(SELECT 1 FROM Domains d WHERE u.UserEmail LIKE '%' + d.Domain)
这篇关于使用SQL,您可以在WHERE LIKE子句中使用子查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文