Results 1 to 2 of 2

Thread: Optimizing query with sub-queries

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Optimizing query with sub-queries

    Hi all,

    Consider the following (My)SQL query I'm using:
    Code:
    	SELECT * 
    	FROM `PageRelations` 
    	WHERE `ancestor` IN (
    		SELECT `descendant` FROM `PageRelations` WHERE `ancestor` = 1
    	)
    	OR `descendant` IN (
    		SELECT `descendant` FROM `PageRelations` WHERE `ancestor` = 1
    	)
    The red marked sub-queries are identical, and I was wondering whether there is a way to only have the sub-query run once?

    Thanks
    Delete it. They just clutter threads anyway.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Optimizing query with sub-queries

    See if this works

    Code:
    SELECT *
    FROM PageRelations PR
    LEFT JOIN PageRelations PR2 on PR2.Ancestor =1 and ((PR.Descendant = PR2.Descendant) or (PR.Ancestor = PR2.Descendant))
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width