在SQL或MySQL中,我们可以连接表和子查询结果吗?
我们是否可以将子查询的结果连接到一个表中,例如:
select name from gifts
LEFT OUTER JOIN (select giftID from gifts) ...
如果不能,是否可以通过一些方法来完成,例如创建临时表?
PS 子查询只能使用 IN 或 NOT IN、EXISTS 或 NOT EXISTS 出现吗?
Can we join a table with the result of a subquery, such as:
select name from gifts
LEFT OUTER JOIN (select giftID from gifts) ...
If not, can it be done by some methods, such as creating a temporary table?
P.S. Can a subquery only appear using IN or NOT IN, or EXISTS or NOT EXISTS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,sql 适用于集合;子查询返回一个集合作为结果,所以这是可能的。
您必须为子查询指定一个名称:
(SELECT * FROM table) AS sub
或(SELECT * FROM table) sub
。作为完整查询:Yes, sql works on sets; a subquery returns a set as result, so this is possible.
You have to give the subquery a name:
(SELECT * FROM table) AS sub
or(SELECT * FROM table) sub
. As full query:是的,您可以使用选择作为内部连接,您只需给它一个别名:
yes you can use a select as an INNER JOIN you just have to give it an alias:
另一种方法是创建子查询的视图。然后像平常一样进行 JOIN(通过引用 VIEW)。
Another way, could be to create a VIEW of the subquery. Then do a JOIN as you would normally would (by referencing the VIEW).