0

我知道有很多关于此的帖子,但我无法使其适用于我的代码。

正如标题所示,我想将来自两个不同数据库的两个表连接在一起。

这是我的代码:

 $dbh1 = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    $dbh2 = mysql_connect("$host2", "$username2", "$password2", true)or die("cannot connect"); 
    mysql_select_db("$db_name", $dbh1)or die("cannot select DB");
    mysql_select_db("$db_name2", $dbh2)or die("cannot select DB");

//first table  
//$sql = mysql_query("SELECT InterestedEntityId, Score FROM users.`user_interests` WHERE UserId= //$userID ORDER BY Score DESC", $dbh1);

//second table
//$sql = mysql_query("SELECT entities.`Name` FROM tags.`entities` WHERE Id = InterestedEntityId", $dbh2);

我想一次性获取 select 语句中提到的 3 个字段(IE InterestedEntityId、Score、entities. Name

关于如何在一个 sql 查询中连接这两个表的任何想法。我尝试使用内部联接并添加表名(如线程建议的那样),但查询没有返回任何内容。

请问有什么想法吗?

4

3 回答 3

0

Something like this should work.

SELECT t1.InterestedEntityId, t1.Score, t2.Name
  FROM DB1.users.`user_interests` t1
  JOIN DB2.tags.`entities` t2 ON t2.UserId = t1.Id

Note: Use PDO as mysql_* is deprecated and not secure enough.

于 2013-04-22T14:15:39.747 回答
0

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.


While it is theoretically possible to join tables from two different databases on the same server, what you are trying to do cannot possibly work because you appear to be accessing two different servers.

In order to get the result set you want you will need to combine them manually.

For example (using PDO):

$dsn1 = "mysql:host=$host;dbname=$db_name";
$dsn2 = "mysql:host=$host2;dbname=$db_name2";

try {
    // Create the connections
    $db1 = new PDO($dsn1, $username, $password);
    $db1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db1->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $db1->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    $db2 = new PDO($dsn2, $username2, $password2);
    $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db2->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $db2->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

    // Get the initial recordset
    $sql1 = "
        SELECT InterestedEntityId, Score
        FROM `user_interests`
        WHERE UserId = :userId
        ORDER BY Score DESC
    ";
    $stmt1 = $db1->prepare($sql1);
    $stmt1->bindParam('userId', $userID, PDO::PARAM_INT);
    $stmt1->execute();

    // Prepare the statement for the second database
    $sql2 = "
        SELECT Name
        FROM entities
        WHERE Id = :entityId
    ";
    $entityId = 0;
    $stmt2 = $db2->prepare($sql2);
    $stmt2->bindParam('id', $entityId, PDO::PARAM_INT);

    // Loop the first result set
    $result = array();
    foreach ($stmt1 as $row1) {
        // Fetch the related data from the second DB
        $entityId = $row1['InterestedEntityId'];
        $stmt2->execute();
        $row2 = $stmt2->fetch();

        // Construct the final result row and store it
        $result[] = array(
            'InterestedEntityId' => $row1['InterestedEntityId'],
            'Score' => $row1['Score'],
            'Name' => $row2['Name']
        );
    }
} catch(PDOException $e) {
    die($e->getMessage());
}

// The result set you want should now be available
var_dump($result);
于 2013-04-22T14:15:48.523 回答
0

Pretty much the model is:

SELECT dbName1.TableName1.ColumnName1, dbName2.TableName2.ColumnName2 FROM dbName1.TableName1 JOIN dbName2.TableName2 ON dbName1.TableName1.ColumnName1 = dbName2.TableName2.ColumnName2
于 2013-04-22T14:16:13.203 回答