MySQL Correlated Subquery Best Practices

Posted on 2014-08-29
Last Modified: 2014-08-29
I've been getting around this type of issue with script instead of handling it with the initial query, and I was wondering how best to approach it from a SQL perspective.
Using this table as an example,
ExampleHow would I design a query to select any ticket names that are like ('Replace PCs%' and IS_PARENT='1') as well as any ticket that has the PARENT_ID of the previously selected tickets? So, basically, how do I return an array containing both parent and children tickets related to a specific ticket name pattern? I would need the array to hold all fields related to the selected records. Thanks for reading.
Question by:thedeal56
    LVL 47

    Accepted Solution

    Here is an approach, but MySQL is quite limited in its capabilities for hierarchical data. The following assumes just a 2 tier hierarchy as depicted.
    from tickets
    where is_parent = 1
    and ticket_name  like 'Replace PCs%'
    union all
    from tickets c
    inner join (
                from tickets
                where is_parent = 1
                and ticket_name  like 'Replace PCs%'
               ) p on c.parent_id =

    Open in new window


    {+ edit}  small note: providing sample data in a re-usable way is appreciated (not images)

    Author Comment

    Thank you so much.  That helps a lot.  Sorry for the static image.
    LVL 47

    Expert Comment

    no problem, it wasn't too much typing, keep it in mind next time
    (using an embedded image is better than an attached image as we don't have to flip between pages)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    I have been using r1soft Continuous Data Protection ( for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now