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'];    
            }    
    }

Link to comment
Share on other sites

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'];    
            }    
    }

Link to comment
Share on other sites

I use it as is, and it works fine, just thought that a Join would be better...

 

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... :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.