0

如何在 Kohana 中连接不同数据库中的表?

$tb_new = 'db_zaboo_feed.feed_' . $feed;
            $ids = DB::query(Database::SELECT, 
                             "SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d
                             LEFT JOIN $tb_new f ON(d.fuid_id = f.uid)
                             WHERE d.user_id = (SELECT user_id FROM $tb_new GROUP BY user_id) AND f.uid IS NULL")->execute();

错误:

Database_Exception [ 1146 ]: Table 'db_zaboo.displays' doesn't exist [ SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d LEFT JOIN db_zaboo_feed.feed_3 f ON(d.fuid_id = f.uid) WHERE d.user_id = (SELECT user_id FROM db_zaboo_feed.feed_3 GROUP BY user_id) AND f.uid IS NULL ]
4

2 回答 2

0

要使用不同的数据库配置组,请将名称或配置对象传递给 execute()。

$result = $query->execute('config_name')

http://kohanaframework.org/3.0/guide/database/query/builder#executing http://kohanaframework.org/3.0/guide/database/config#connection-settings

于 2014-09-01T22:18:30.537 回答
0

这不是 Kohana 特定的问题。这是一个 MySQL 问题。

要在同一个查询中对多个数据库进行操作,您必须使用表名及其数据库名。如:

SELECT * FROM database1.table1 WHERE database1.table1.id IN (SELECT table1_id FROM database2.table2)

请注意:通过逐字编写您的查询,我不确定 Kohana 的查询构建器是否能像使用查询构建器的特定功能构建 SQL 查询那样防止 SQL 注入攻击。我会使用函数mysql_real_escape_string来阻止它

于 2014-09-01T22:33:48.947 回答