我需要执行这个SQL查询:
SELECT DISTINCT
logger.hcp_id,
logger.rep_id,
logger.type,
session_brand_presentation.ID,
session_brand_presentation.brand_id,
session_brand_presentation.createdAt,
session_brand_presentation.modifiedAt
FROM
archive_pfizer.logger
JOIN
pdone_legacy.session_brand_presentation ON logger.session_id = session_brand_presentation.local_session_id
WHERE
logger.type = 'email_sent';
您可能已经注意到我正在查询不同的数据库:archive_pfizer
和pdone_legacy
。我知道DSN需要一个数据库名称来创建PDO对象,然后标题说:当同一个查询中有两个数据库涉及时,如何执行PDO语句?
非常重要我在问如何使用PDO从PHP实现这一点,我能够从MySQL / MariaDB命令行和/或使用任何GUI成功执行查询。
更新
以下是我根据 @RyanVincent 回答的代码:
$config = parse_ini_file('config.ini', true);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
$DBASE = $config['database']['DBASE'][0];
$pdo = setupDB($config, $DBASE);
$sql = 'SELECT DISTINCT logger.hcp_id, logger.rep_id, logger.type, session_brand_presentation.ID, session_brand_presentation.brand_id, session_brand_presentation.createdAt, session_brand_presentation.modifiedAt FROM archive_pfizer.logger JOIN pdone_legacy.session_brand_presentation ON logger.session_id = session_brand_presentation.local_session_id WHERE logger.type = "email_sent"';
foreach($pdo->query($sql) as $row) {
var_export($row);
echo EOL;
}
但我收到了这个错误:
PHP致命错误:带有消息的未捕获异常'PDOException' 'SQLSTATE [42S02]:找不到基表或视图:1146表 'pdone_legacy.session_brand_presentation'不存在'
显然,当它是一个数据库时,它将pdone_legacy.session_brand_presentation
作为一个表,在提供的示例中与testmysql
相同,是否有任何建议?
更新2
尝试相同的SQL查询但使用别名也不起作用:
$sql = 'SELECT DISTINCT
lg.hcp_id,
lg.rep_id,
lg.type,
sbp.ID,
sbp.brand_id,
sbp.createdAt,
sbp.modifiedAt
FROM
archive_pfizer.logger AS lg
JOIN
pdone_legacy.session_brand_presentation AS sbp ON lg.session_id = sbp.local_session_id
WHERE
lg.type = "email_sent"';
与以前完全相同。
更新3
好吧,也许我会在说完之后被杀,但是我一直都很糟糕。我正在连接到存在数据库pdone_legacy
的服务器,但该数据库实际上没有session_brand_presentation
表,这就是PDO一直在说的内容。无论如何,感谢这里的任何人,这两个查询都是有效的。
答案 0 :(得分:1)
以下是使用PDO从两个单独的mysql数据库加入的代码。
限制:
使用archive_pfizer(logger)和pdone_legacy(session_brand_presentation)进行查询
$sqlBoth = 'SELECT DISTINCT
logger.hcp_id,
logger.rep_id,
logger.type,
session_brand_presentation.ID,
session_brand_presentation.brand_id,
session_brand_presentation.createdAt,
session_brand_presentation.modifiedAt
FROM
archive_pfizer.logger
JOIN
pdone_legacy.session_brand_presentation
ON logger.session_id = session_brand_presentation.local_session_id
WHERE
logger.type = :lg_type';
$stmt = $dbTest->prepare($sqlBoth);
$stmt->bindValue(':lg_type', 'email_sent', PDO::PARAM_STR);
$stmt->execute();
$resultBoth = $stmt->fetchAll();
$stmt->closeCursor();
输出:
pdone_legacy and archive_pfizer
Array
(
[0] => Array
(
[hcp_id] => hcp_id_01
[rep_id] => rep_od_01
[type] => email_sent
[ID] => ID_01
[brand_id] => brand_id_01
[createdAt] => 2015-09-24 01:42:51
[modifiedAt] =>
)
)
数据库连接:
/**
* must have access rights to both db's
*/
// db connection to archive_pfizer and pdone_legacy
$dsn = 'mysql:host=localhost;dbname=pdone_legacy';
$username = 'pfizer';
$password = 'pfizer';
$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);
测试我们可以访问单独的数据库......
1)archive_pfizer(logger)
/* ----------------------------------------------------------
* Query archive_pfizer (logger)
*/
$sqlArchive = 'SELECT hcp_id, rep_id, type, session_id, createdAt, modifiedAt
FROM archive_pfizer.logger
WHERE session_id = :a_session_id';
$stmt = $dbTest->prepare($sqlArchive);
$stmt->bindValue(':a_session_id', 'session_id_01', PDO::PARAM_STR);
$stmt->execute();
$resultArchive = $stmt->fetchAll();
$stmt->closeCursor();
echo '<br />', 'archive_pfizer.logger', '<br />';
echo '<pre>';
print_r($resultArchive);
echo '</pre>';
2)pdone_legacy(session_brand_presentation)
/* --------------------------------------------------
* Query pdone_legacy (session_brand_presentation)
*/
$sqlPDone = 'SELECT ID,
local_session_id,
brand_id,
createdAt,
modifiedAt
FROM pdone_legacy.session_brand_presentation
WHERE local_session_id = :sbp_session_id';
$stmt = $dbTest->prepare($sqlPDone);
$stmt->bindValue(':sbp_session_id', 'session_id_01', PDO::PARAM_STR);
$stmt->execute();
$resultPDone = $stmt->fetchAll();
$stmt->closeCursor();
echo '<br />', 'pdone_legacy.session_brand_presentation', '<br />';
echo '<pre>';
print_r($resultPDone);
echo '</pre>';
答案 1 :(得分:0)
您将无法直接使用PDO进行此操作。 PDO只是数据库连接的抽象。
如果您的两个数据库中有一个是PostgreSQL数据库,则可以设置Foreign Data Wrapper。使用MySQL,您应该检查Federated storage engine。