0

does anyone know the best solution to join this query into 1 query rather than 2 as i cannot get it working you see.

function fn_order_category_get_order_info($cat_id)
{
    $id1 = db_query("SELECT category_id FROM ?:products_categories WHERE product_id = $cat_id");
    $id2 = db_query("SELECT category FROM ?:category_descriptions WHERE category_id = $id1");
    return $id2;
}

Thanks in advance!

1 Answer 1

2

Try this:

SELECT 
    pc.category_id,
    cd.category 
FROM
    products_categories pc 
    LEFT JOIN
    category_descriptions cd 
    ON pc.category_id = cd.category_id 
WHERE product_id = $product_id ;
6
  • Database error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN category_descriptions cd ON pc.category_id = cd.category_id' at line 2 (1064) Invalid query: SELECT category_id, category FROM products_categories pc WHERE product_id = 138 LEFT JOIN category_descriptions cd ON pc.category_id = cd.category_id
    – james
    Commented Dec 12, 2010 at 23:25
  • @james Sorry, just put the where to the end of the query (edited). Commented Dec 12, 2010 at 23:31
  • @styu - it seems that im getting this now: Database error: Column 'category_id' in field list is ambiguous (1052) Invalid query: SELECT category_id, category FROM cscart_products_categories pc LEFT JOIN cscart_category_descriptions cd ON pc.category_id = cd.category_id WHERE product_id = 138;
    – james
    Commented Dec 13, 2010 at 0:35
  • @styu: i have fixed it but its returning th category_id rather than the name assigned to the category_id which would be "category"
    – james
    Commented Dec 13, 2010 at 0:53
  • function fn_order_category_get_order_info($product_id) { $result = db_get_field("SELECT pc.category_id, cd.category FROM ?:products_categories pc LEFT JOIN ?:category_descriptions cd ON pc.category_id = cd.category_id WHERE product_id = $product_id;"); return $result; }
    – james
    Commented Dec 13, 2010 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.