[英]Query from multiple databases
I have a ticket system I set up for multiple websites. 我有为多个网站设置的票务系统。 Each table in the database is the same on all website.
数据库中的每个表在所有网站上都是相同的。
My question is how would I query the multiple tables from each database? 我的问题是如何从每个数据库查询多个表?
Example: 例:
I need to write a query that will tell me what is the most recent ticket submitted. 我需要写一个查询,告诉我最近提交的票证是什么。
So I want to know where the ticket came from and the time it was posted. 因此,我想知道票的来源和时间。
Here is what I have tried but need some guidance as I've never worked with multiple databases before. 这是我尝试过的方法,但是需要一些指导,因为我以前从未使用过多个数据库。
$database_1 = 'dogs';
$database_2 = 'cats';
$recent = DB::getInstance()->query("
SELECT `st_id`,`dates`,`complex` FROM {$database_1}.`support_ticket` WHERE `status` = 'OPEN' ORDER BY `dates` DESC LIMIT 1
UNION ALL
SELECT `st_id`,`dates`,`complex` FROM {$database_2}.`support_ticket` WHERE `status` = 'OPEN' ORDER BY `dates` DESC LIMIT 1 ");
foreach($recent->results() as $r):
?>
<div class="box-bottom">Most Recent: <?php echo escape($r->complex); ?> -
<?php echo escape (date("F d, Y - h:i a", strtotime ($r->dates))); ?></div>
<?php endforeach ?>
Just use one ORDER BY statement 只需使用一个ORDER BY语句
SELECT `st_id`,`dates`,`complex`
FROM {$database_1}.`support_ticket`
WHERE `status` = 'OPEN'
UNION ALL
SELECT `st_id`,`dates`,`complex`
FROM {$database_2}.`support_ticket`
WHERE `status` = 'OPEN'
ORDER BY `dates` DESC LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.