PHP/MySQL Query Problem - Not querying properly

Discussion in 'PHP' started by Python, Feb 28, 2008.

  1. #1
    Hello all,

    I'm having a little trouble with an SQL query. My script consists of the following:

    Parent/child categories (Top level categories have column 'parent' = 0).
    Data items held within child categories

    In the data table I have the columns 'topparent' (the top level category) and 'childparent' (the child category it is in).

    In the categories table there is the column 'string' which I need to extract. The idea is to list all data items - From this query I need to be able to use the 'string' of the 'topparent' category and the string of the 'childparent' category.

    So far I've only been able to output just one of the 'string' values and cannot quite figure out how to get both of them.

    Any help would be much apperciated.

    Thanks
     
    Python, Feb 28, 2008 IP
  2. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I assume you have a table
    [category_id_parent, category_id_child, other_data]
    and want category->string for both ids ?

    $sql = ("SELECT
    a.data,
    c1.string,
    c2.string
    FROM
    datatable AS a
    JOIN
    categories AS c1
    ON
    a.topparent = c1.categoryid
    JOIN
    categories AS c2
    ON
    a.childparent = c2.categoryid";

    something like that ?
     
    juust, Feb 29, 2008 IP
  3. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #3
    By using that method I have the following.


    SELECT
    	tutorial.ID,
    	tutorial.type,
    	tutorial.string,
    	tutorial.name,
    	tutorial.description,
    	tutorial.url,
    	tutorial.parent,
    	tutorial.date_added,
    	tutorial.avatar,
    	C1.string,
    	C2.string,
    	user.username
    FROM
    	tutorial, user
    JOIN
    	tutorial_cat AS C1
    ON
    	tutorial.topparent = C1.ID
    JOIN
    	tutorial_cat AS C2
    ON
    	tutorial.parent = C2.ID
    WHERE
    	tutorial.active = 1
    	&& user.ID = tutorial.authorID
    ORDER BY
    	tutorial.date_added DESC
    
    Code (markup):
    The error Im getting is 'Cant execute query Unknown column 'tutorial.topparent' in 'on clause''.

    The table tutorial does have a column called 'topparent'.

    Just to clarify:
    'topparent' = top level category
    'parent' = child level category

    Thanks
     
    Python, Feb 29, 2008 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    SoKickIt, Mar 1, 2008 IP
  5. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #5
    It works great :D
    Thanks to both of you.
     
    Python, Mar 1, 2008 IP