简体   繁体   中英

MySQL select query in where clause

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.

You need a from clause before the 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.

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:

  SELECT DISTINCT t.*
    FROM some_table t, ut_funds u
   WHERE t.id=u.unit_trust_managing_company_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM