Jump to content

2 different databases, 2 different tables --> JOIN


webent

Recommended Posts

Can someone please help me with the syntax of how to join 2 different databases with 2 different tables? Also, the table name of second query is dynamic and thus, a variable. The common shared field name of both tables is the SKU.

 

Here's my code for the two queries,...

 

$result = mysql_query("SELECT SKU FROM domain_supplier.featured_products WHERE featured_products_id = '1'", $b_link);
    while ($row = mysql_fetch_assoc($result)) {
        $SKU = $row['SKU'];
        $sub_result = mysql_query("SELECT PRODUCT_IMAGE, MSRP, PRODUCT_NAME FROM domain_supplier.$single_product_line ORDER BY RAND() WHERE SKU = '$row[sKU]'", $a_link);
            while ($sub_row = mysql_fetch_assoc($sub_result)) {
                $PRODUCT_IMAGE = $sub_row['PRODUCT_IMAGE'];
                $MSRP = $sub_row['MSRP'];
                $PRODUCT_NAME = $sub_row['PRODUCT_NAME'];    
            }    
    }

My time ran out for me to edit my last post, but I had a very small syntaxical error... On the first line of the first query, the table should be "$local_database.featured_products" and the "WHERE" clause removed... here is the corrected query in total...

 

$result = mysql_query("SELECT SKU FROM $local_database.featured_products", $b_link);
    while ($row = mysql_fetch_assoc($result)) {
        $SKU = $row['SKU'];
        $sub_result = mysql_query("SELECT PRODUCT_IMAGE, MSRP, PRODUCT_NAME FROM domain_supplier.$single_product_line ORDER BY RAND() WHERE SKU = '$row[sKU]'", $a_link);
            while ($sub_row = mysql_fetch_assoc($sub_result)) {
                $PRODUCT_IMAGE = $sub_row['PRODUCT_IMAGE'];
                $MSRP = $sub_row['MSRP'];
                $PRODUCT_NAME = $sub_row['PRODUCT_NAME'];    
            }    
    }

If you wouldn't mind too awful much then, could you please tell them to fix it so that we can use dynamic table names in Joins... :)

It's virtually impossible to implement this feature -- it would make calculating the join plan practically impossible.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.