Dinuka Thilanga
Dinuka Thilanga

Reputation: 4330

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

Upvotes: 6

Views: 55570

Answers (2)

Cuse70
Cuse70

Reputation: 381

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

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269445

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.

Upvotes: 22

Related Questions