Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

LEFT OUTER JOIN for parent-sub items

$
0
0
Hi,

This is how for I could get with LEFT OUTER JOIN, what's the proper way of doing this:

Code:

$sql = "
        SELECT
        menu1.title as title1,
        menu2.title as title2
       
        FROM menu_links AS menu1

        LEFT OUTER JOIN menu_links AS menu2
                ON menu1.menu_link_id = menu2.parent_link_id                               

        WHERE menu1.parent_link_id = 0";

$stat = mysql_query($sql, $dblink);
while ($row = mysql_fetch_array($stat)) {
        $list .= "Title1: ".$row["title1"]."<br>";
        if ($row["title2"]) {
                $list .= "-- Title2: ".$row["title2"]."<br>";
        }
}

It produces:

Title1: About Us
Title1: Articles
-- Title2: Fruit
Title1: Articles
-- Title2: Meet
Title1: Articles
-- Title2: Vegetables
Title1: Donations
Title1: Services
Title1: Volunteers
Title1: News & Events
Title1: Home
Title1: Contact Us

'Articles' needs to be printed once as a parent of Fruit, Meet and Vegetables. And how do I mention in the query not to give empty values which I'm avoiding using if condition?

I will appreciate any sort of help in this matter.

Viewing all articles
Browse latest Browse all 13329

Trending Articles