Jump to content

[SOLVED] Joining tables


wsantos

Recommended Posts

Is it possible to join two tables from two different connection?

$con1 = mysql_connect("ip1","user","pwd");
  if(!$con1)
  {
    die('Could not connect: ' . mysql_error());
  }
  $db1 = mysql_select_db("table1",$con1);

  $con2 = mysql_connect("ip2","user","pwd");
  if(!$con1)
  {
    die('Could not connect: ' . mysql_error());
  }
  $db2 = mysql_select_db("table2",$con2);

Link to comment
Share on other sites

like this?

 

 

 

select table2.uniqueid,table3.action_id,table3.action from db2.table3 join db2.table2 join db1.table1 where table3.action = table2.action and table1.uniqueid = table2.uniqueid limit 30

 

or do i put the db name in the fieldnames like this?

 

select db2.table2.uniqueid,db2.table3.action_id,db2.table3.action from db2.table3 join db2.table2 join db1.table1 where table3.action = table2.action and table1.uniqueid = table2.uniqueid limit 30

Link to comment
Share on other sites

the second one. you need to put that database before everything when pulling from multiple databases.

 

made some slight changes to query.

select db2.table2.uniqueid,db2.table3.action_id,db2.table3.action from db2.table3, db2.table2, db1.table1 where DB2.table3.action = DB2.table2.action and DB1.table1.uniqueid = DB2.table2.uniqueid limit 30

 

why are you joining db1.table1 when you are not pulling any data from it? it's ambiguous.

 

EDIT:

it's also possible to use aliases

 

select tb2.uniqueid,tb3.action_id,tb3.action from db2.table3 as tb3, db2.table2 as tb2, db1.table1 as tb1 where tb3.action = tb2.action and tb1.uniqueid = tb2.uniqueid limit 30

Link to comment
Share on other sites

here is my complete connection and query as it appears on my script except for the ip,username, and password

 

// Connect to CDR Table
  $conCDR = mysql_connect("ip1","user","pwd");
  if(!$conCDR)
  {
    die('Could not connect: ' . mysql_error());
  }

// Connect to CDR Table
  $conAsteriskCDR = mysql_connect("ip2","user","pwd");
  if(!$conAsteriskCDR)
  {
    die('Could not connect: ' . mysql_error());
  }

$query = "SELECT cdr.cdr.uniqueid, cdr.cdr.clid, asteriskcdr.lookup_actions.action_id, asteriskcdr.lookup_actions.action from  asteriskcdr.lookup_actions join asteriskcdr.queue_actions join cdr.cdr where asteriskcdr.lookup_actions.action = asteriskcdr.queue_actions.action and cdr.cdr.uniqueid = asteriskcdr.queue_actions.uniqueid and cdr.cdr.clid like '%" . $Group . " G%' and cdr.cdr.start >= '$startdate' and end <= '$enddate' ORDER BY cdr.cdr.cdrid;

mysql_close($conCDR);
mysql_close($conAsteriskCDR);

 

 

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.