我需要带有 JOIN 的 SELECT,但目前不知道如何执行此操作。我已经阅读了一些教程,但它并不能帮助我解决我的问题。
我在 2 个数据库中有 2 个表:
DATABASE A
-> CONTENT
--> | PARTNERID | PAYSITE |
--> | 1 | siteA.com |
--> | 2 | siteA.com |
--> | 3 | siteA.com |
--> | 3 | siteB.com |
DATABASE B
-> WMIDPP
--> | WMID | PARTNERID | PARTNERURL | PAYSITE | ACTIVE
--> | 1 | 1 | AFFLINK 1 | siteA.com | 1
--> | 1 | 2 | AFFLINK 2 | siteA.com | 1
--> | 2 | 1 | AFFLINK 1 | siteA.com | 1
上面的表包含更多字段,但我需要使用这些字段。
如果我进入网站,我已经知道变量 $WMID 和 $PAYSITE。当我进入网站时,它应该只显示以下数据:
来自表 CONTENT 和表 WMIDPP 的字段 PARTNERURL 的完整数据
CONTENT.PARTNERID = WMIDPP.PARTNERID
and
WMIDPP.WMID = $WMID
and
WMIDPP.PAYISTE = $PAYSITE
but only if
WMIDPP.ACTIVE = 1
任何人都可以帮助解决我的问题吗?
提前致谢
托斯顿
编辑:
这里有一些有关数据库和表的更多信息:
表 CONTENT 位于数据库 A 中,表 WMIDPP 位于数据库 B 中。
我像这样访问这些数据库:
$DB_HOST = "localhost";
$DB_NAME1 = "database1";
$DB_NAME2 = "database2";
$DB_USER = "username";
$DB_PASS = "password";
$OPTION = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
try {
$dbAS = new PDO("mysql:host=" . $DB_HOST . ";dbname=" . $DB_NAME1, $DB_USER, $DB_PASS, $OPTION);
$dbEC = new PDO("mysql:host=" . $DB_HOST . ";dbname=" . $DB_NAME2, $DB_USER, $DB_PASS, $OPTION);
}
catch (PDOException $e) { exit("Verbindung fehlgeschlagen! " . $e->getMessage()); }
用户有权访问这两个数据库。
我有 2 个不同的数据库,因为有 2 个不同的项目。如果它不适用于这些配置,我可以将表从数据库 B 移动到数据库 A - 但这应该只是最坏情况的选择。我更喜欢合并两个不同数据库中的表数据的解决方案。
最佳答案
您使用一个 PDO 连接。
登录详细信息必须允许访问您想要访问的两个数据库。
任何列的完整“路径”是:
<database name> . <table name> . <column name>.
下面是经过测试的代码,用于连接两个独立数据库中的表。
数据库 1) 名为:'testmysql'
数据库 2) 称为:“rags1”
它们只是我在这里设置的数据库,上面有类似的结构化表。
连接来自不同数据库的两个表
/* --------------------------------------------------------------------
* Query testmysql and rags1
*/
$sqlTest = 'SELECT rags1.user.id as RagsRowId,
rags1.user.userid as RagsUserId,
testmysql.customer.id as TestRowId,
testmysql.customer.custid as TestCustId,
testmysql.customer.address as TestCustAddress
FROM testmysql.customer
JOIN rags1.user
ON testmysql.customer.custid = rags1.user.userid
AND testmysql.customer.custid = :testCustId';
$stmt = $dbTest->prepare($sqlTest);
$stmt->bindValue(':testCustId', 'rfv123', PDO::PARAM_STR);
$stmt->execute();
$testResult = $stmt->fetchAll();
$stmt->closeCursor();
echo '<pre>';
print_r($testResult);
echo '<pre>';
连接详细信息:
/**
* Two databases :
* 1) testmysql
* 2) rags1
*
* Use the 'database' name that you want to refer to in the queries.
*/
/**
* must have access rights to both db's
*/
// db connection to TestMysql and Rags1
$dsn = 'mysql:host=localhost;dbname=testmysql';
$username = 'rfv123';
$password = 'rfv123';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbTest = new PDO($dsn, $username, $password, $options);
$dbTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
输出:
Array
(
[0] => Array
(
[RagsRowId] => 1
[0] => 1
[RagsUserId] => rfv123
[1] => rfv123
[TestRowId] => 3
[2] => 3
[TestCustId] => rfv123
[3] => rfv123
[TestCustAddress] => 123 Somewhere Street, Cheshire
[4] => 123 Somewhere Street, Cheshire
)
)
关于mysql - 需要 mySQL PDO JOIN 语句的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394190/