SSP类PHP上两个数据库连接的单一查询

siv3szwd  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(124)

我想从2个数据库检索数据,这是从同一个服务器上的单一查询。
下面是我想要实现的查询。

SELECT Customer.id, Receipt.Status,
(SELECT b.created_date FROM systemB.History as b WHERE b.id_number LIKE Customer.id 
ORDER BY id DESC LIMIT 1) as Date 
FROM Customer
LEFT JOIN Receipt ON Receipt.Card_No = Customer.id
WHERE Receipt.Status = 'PAID

其结果如下。

'+--------+--------+--------------+
|   id    | Status |    Date      |
+---------+--------+--------------+            
|   1     | PAID   |   2023-02-01 |  
|   2     | PAID   |   2022-07-05 |  
+--------+---------+--------------+

system表的数据库--〉客户,收货
表的systemB数据库--〉历史记录
基本上,两个连接在同一个服务器上使用相同的用户名和密码.我如何实现下面的查询server_processing. php

(SELECT b.created_date FROM systemB.History as b WHERE b.id_number LIKE Customer.id 
ORDER BY id DESC LIMIT 1) as Date
    • 服务器处理. php**
<?php
    
    $table = 'Customer'; // DB table to use
    
    $primaryKey = 'id'; // Table's primary key
    
    // The `db` parameter represents the column name in the database, while the `dt`
    // parameter represents the DataTables column identifier. In this case simple
    // indexes
    $columns = array(
        array( 'db' => '`c`.`id`',  'dt' => 'id', 'field' => 'id' ),
        array( 'db' => '`r`.`Status`', 'dt' => 'Status', 'field' => 'Status' )
    );
    
    $sql_details = array( ); // sql connection

    require('ssp.customized.class.php' );
    $joinQuery = "FROM `Customer` AS `c` 
                  LEFT JOIN `Receipt` AS `r` ON (`c`.`id` = `r`.`Card_No`)"; 

    $where = "`r`.`Status` = 'PAID'";
    
    echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery, $where)
); ?>

我试过了。它给我这个错误。

$joinQuery = "FROM `Customer` AS `c` 
              LEFT JOIN `Receipt` AS `r` ON (`c`.`id` = `r`.`Card_No`)
              LEFT JOIN `systemB.History` AS `b` ON (`c`.`id` = `b`.`id_number`)
              ";
  • 发生SQL错误:数据库状态[42S02]:未找到基表或视图:1146表格'系统A.系统B.历史记录'不存在 *

如何实现查询?如有帮助,将不胜感激。

x8diyxa7

x8diyxa71#

在MySQL中,反号是标识符的引号字符。这意味着反号中的任何内容都被视为一个标识符。只有当标识符中有特殊字符时才需要反号。
在您的查询中,您有:

`systemB.History`

这意味着MySQL正在从当前(systemA)数据库中查找表systemB.History
您可以改为用途:

systemB.History

或者,如果要使用反勾号,则:

`systemB`.`History`

相关问题