来自不同数据库

时间:2017-11-11 13:03:59

标签: mysql database join

我有两个独立的MySQL查询,每个查询使用不同的数据库,来自同一台服务器。

使用database1查询:

$result1 = mysqli_query($db_connection1, "
SELECT p.id, p.identifier, u.star_total FROM pages AS p
    LEFT JOIN ( 
        SELECT page_id, AVG(rating) star_total FROM (
            SELECT page_id, rating FROM comments WHERE comments.is_approved = '1' AND comments.rating != '0' 
            UNION ALL 
            SELECT page_id, rating FROM ratings) d
            GROUP BY page_id
    ) AS u ON p.id = u.id
");

使用database2查询:

$result2 = mysqli_query($db_connection2, "
SELECT 
    cas.id, cas.casino,  
    t2.amount, t2.match,  
    t3.bonus_id, t3.bonus_type 
FROM (SELECT * FROM casinos $query) AS cas 
LEFT JOIN (  
        bonus AS t2
    INNER JOIN 
        bonus_type_lookup AS t3 ON t2.bonus_type = t3.bonus_id
)
ON cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo'
AND (
    t2.amount = (
        SELECT 
            CASE WHEN MAX(t2.amount) > MAX(t2.match) OR (MAX(t2.match) IS NULL) THEN MAX(t2.amount) 
            ELSE MAX(t2.match) 
            END 
        FROM bonus AS t2 
        WHERE cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo' 
    ) 
    OR t2.match = (
        SELECT 
            CASE WHEN MAX(t2.amount) > MAX(t2.match) 
            THEN MAX(t2.amount) 
            ELSE MAX(t2.match) 
            END 
        FROM bonus AS t2
        WHERE cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo' 
    )
)
ORDER BY $order_by $order $limit
");

两个查询都可以自行完成。现在我想LEFT JOIN这两个查询。我当前的尝试(见下文)产生了这个错误:mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given,它引用了这一行:$numrows = mysqli_num_rows($result);

$result = mysqli_query($db_connection2, "
SELECT 
    cas.id, cas.casino,  
    t2.amount, t2.match,  
    t3.bonus_id, t3.bonus_type,
    **j.identifier, j.star_total**  
FROM (SELECT * FROM database2.casinos $query) AS cas 
LEFT JOIN (
        database2.bonus AS t2
    INNER JOIN 
        database2.bonus_type_lookup AS t3 ON t2.bonus_type = t3.bonus_id
)
ON cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo' 
AND (
    t2.amount = (
        SELECT 
            CASE WHEN MAX(t2.amount) > MAX(t2.match) OR (MAX(t2.match) IS NULL) THEN MAX(t2.amount) 
            ELSE MAX(t2.match) 
            END 
        FROM database2.bonus AS t2 
        WHERE cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo' 
    ) 
    OR t2.match = (
        SELECT 
            CASE WHEN MAX(t2.amount) > MAX(t2.match) 
            THEN MAX(t2.amount) 
            ELSE MAX(t2.match) 
            END 
        FROM database2.bonus AS t2
        WHERE cas.id = t2.id AND t2.bonus_type = '$bonus_typeNo' 
    )
)

LEFT JOIN (
    SELECT 
        p.id, 
        p.identifier, 
        q.star_total 
    FROM database1.pages p
    LEFT JOIN ( 
        SELECT 
            t.page_id, 
            AVG(t.rating) star_total 
        FROM ( 
            SELECT 
                c.page_id AS page_id, 
                c.rating AS rating 
            FROM database1.comments c 
            WHERE c.is_approved = '1' AND c.rating != '0'
            UNION ALL
            SELECT 
                r.page_id AS page_id, 
                r.rating AS rating 
            FROM database1.ratings r
        ) t
        GROUP BY t.page_id
    ) q ON q.page_id = p.id
) **j ON cas.id = j.identifier**  

ORDER BY $order_by $order $limit
");
$numrows = mysqli_num_rows($result);

我不确定错误是由错误使用LEFT JOIN还是由于这是数据库问题,或两者兼而有之。 “$ db_connection2”使用database2。不确定它连接到database1的哪个阶段,以及它如何获得使其能够连接到database1的连接细节。我试图为每个表和数据库使用完全限定名称,但没有成功。

任何帮助都会很棒。先感谢您!

1 个答案:

答案 0 :(得分:1)

您收到以下错误消息:

  

每个派生表都必须有自己的别名

错误消息表示在FROM子句中使用子查询要求您为子查询提供表别名:

SELECT ... FROM ( ...subquery...) AS t1 ... 

这样您就可以引用子查询的列,如t1.column1

此处记录: https://dev.mysql.com/doc/refman/5.7/en/derived-tables.html

  

[AS] tbl_name子句是必需的,因为FROM子句中的每个表都必须有一个名称。