MySQL Forums
Forum List  »  Newbie

Re: Convert Query from using Subqueries, compatible with MySQL 3.23
Posted by: Sean Nolan
Date: April 04, 2005 09:49PM

The example you gave can be rewritten (and should be) as a join.

select g.id, g.name, g.description, p.permission
from groups g left join permissions p on g.id=p.group_id or p.group_id Is Null
where p.id=3 or p.id is null;

In general, you can usually do away with subqueries by using joins, joins with group by, or by creating a temporary table that you then join to the main query.

To create a temporary table, you can use this
CREATE TEMPORARY TABLE tempSubQuery SELECT ....
where the SELECT is pretty much the subquery (include the columns in the SELECT that will join to the main query).

Sean

Options: ReplyQuote


Subject
Written By
Posted
Re: Convert Query from using Subqueries, compatible with MySQL 3.23
April 04, 2005 09:49PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.