MySQL 在 where 子句中选择查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18548480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL select query in where clause
提问by Dinuka Thilanga
Can u correct this query? it is show some syntax error.
你能更正这个查询吗?它显示一些语法错误。
SELECT *
WHERE `id` IN (SELECT DISTINCT unit_trust_managing_company_id
FROM ut_funds
ORDER BY `company_name`)
There SELECT DISTINCT unit_trust_managing_company_id FROM ut_funds ORDER BY
company_name` query is working properly.
有SELECT DISTINCT unit_trust_managing_company_id FROM ut_funds ORDER BY
company_name` 查询工作正常。
回答by Gordon Linoff
You need a from
clause before the where
:
from
在 之前需要一个子句where
:
SELECT *
FROM <some table here>
WHERE `id` IN (SELECT unit_trust_managing_company_id FROM ut_funds)
Also, the distinct
and order by
are not needed for the in
statement.
此外,该语句不需要distinct
和。order by
in
回答by Cuse70
In most (all?) cases you can rewrite the query without using the query in the where clause AND get much better performance. Try something like:
在大多数(所有?)情况下,您可以在不使用 where 子句中的查询的情况下重写查询,并获得更好的性能。尝试类似:
SELECT DISTINCT t.*
FROM some_table t, ut_funds u
WHERE t.id=u.unit_trust_managing_company_id