Mysql - 我可以获取子选择结果并在 WHERE 子句中使用它吗?
我想在 where 子句中获取并使用从 MySQL 中的子选择返回的值。 这可能吗? 似乎没有必要将子查询写出两次——但是如果我需要的话,MySQL 是否足够聪明,只运行一次?
我尝试了以下方法,但不起作用:
SELECT
(SELECT 1 FROM table WHERE somereallycomplicatedclause = 'something')
AS subselectresult
FROM content WHERE subselectresult = 1
这会产生此错误:
#1054 - Unknown column 'subselectresult' in 'where clause'
谢谢
I want to both fetch and use in a where clause the value returned from a sub-select in MySQL. Is this possible? It seems unecessary to write the sub-query out twice - however if I need to, will MySQL be clever enough to only run it one?
I have tried the following which does not work:
SELECT
(SELECT 1 FROM table WHERE somereallycomplicatedclause = 'something')
AS subselectresult
FROM content WHERE subselectresult = 1
This generates this error:
#1054 - Unknown column 'subselectresult' in 'where clause'
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能在 WHERE 子句中使用列别名。 尝试将其放入 HAVING 中,然后它应该可以工作。
这里的解释: http://dev.mysql.com /doc/refman/5.0/en/problems-with-alias.html
you can't use column aliases in a WHERE clause. try putting it in HAVING and it should work then.
Explanation here: http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
您显示的查询很奇怪。 您可以通过这种方式获得相同的结果(交叉产品):
我认为您应该更多地说明您尝试解决的问题(例如,子查询和联系人表之间的链接是什么)?
Your query as you displayed is strange. You can achieve the same result this way (cross product):
I think you should say more about what do you try to solve (e.g. what is the link between the sub query and the contacts table)?