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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:42:43  来源:igfitidea点击:

MySQL select query in where clause

mysqlsqlselectwhere

提问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 BYcompany_name` query is working properly.

SELECT DISTINCT unit_trust_managing_company_id FROM ut_funds ORDER BYcompany_name` 查询工作正常。

回答by Gordon Linoff

You need a fromclause before the where:

from在 之前需要一个子句where

SELECT *
FROM <some table here>
WHERE `id` IN (SELECT unit_trust_managing_company_id FROM ut_funds)

Also, the distinctand order byare not needed for the instatement.

此外,该语句不需要distinct和。order byin

回答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