0

我正在使用 2 个数据库,我需要找到与最接近的时间匹配的记录。这两个字段都是 datetime()。

所以本质上:

table1.time = 2012-06-07 15:30:00
table2.time = 2012-06-07 15:30:01
table2.time = 2012-06-07 15:30:02
table2.time = 2012-06-07 15:30:03

注意:我正在查询的表 (table2) 是一个 mssql 表,而 table1.time 是一个 datetime() 时间。我需要在 table2 中找到与 table1.time 最接近的行,但我不能保证它会完全匹配,所以我需要最接近的。我只需要返回 1 个结果。

我根据上一个 stackoverflow 查询中的示例尝试了下面的 SQL,但它无法正常工作。

Table1 是一个 mysql 数据库,其中 table2 是 mssql,查询发生在 table2 (mssql)

  try {
            $sql = "
                SELECT     
                    PCO_AGENT.NAME, 
                    PCO_INBOUNDLOG.LOGIN AS LOGINID, 
                    PCO_INBOUNDLOG.PHONE AS CALLERID, 
                    PCO_INBOUNDLOG.STATION AS EXTEN, 
                    PCO_INBOUNDLOG.TALKTIME AS CALLLENGTH, 
                    PCO_INBOUNDLOG.CHANNELRECORDID AS RECORDINGID, 
                    PCO_SOFTPHONECALLLOG.RDATE, 
                    PCO_INBOUNDLOG.RDATE AS INBOUNDDATE
                FROM         
                    PCO_INBOUNDLOG 
                INNER JOIN
                      PCO_LOGINAGENT ON PCO_INBOUNDLOG.LOGIN = PCO_LOGINAGENT.LOGIN 
                INNER JOIN
                      PCO_SOFTPHONECALLLOG ON PCO_INBOUNDLOG.ID = PCO_SOFTPHONECALLLOG.CONTACTID 
                INNER JOIN
                      PCO_AGENT ON PCO_LOGINAGENT.AGENTID = PCO_AGENT.ID
                WHERE 
                    PCO_INBOUNDLOG.STATION = :extension
                AND ABS(DATEDIFF(:start,PCO_SOFTPHONECALLLOG.RDATE))
            ";
            $arr = array(":extension" => $array['extension'], ":start" => $array['start']);
            $query = $this->mssql->prepare($sql);
            $query->execute($arr);
            $row = $query->fetchAll(PDO::FETCH_ASSOC);
            $this->pre($row);
        }

我目前收到以下错误:

SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]SQLSTATE[HY000]: General error: 174 General SQL Server error: Check messages from the SQL Server [174] (severity 15) [(null)]
4

1 回答 1

0

找到一个较短的版本:

 SELECT * FROM `table` WHERE `date` < '$var' ORDER BY date LIMIT 1;
于 2012-06-11T10:58:16.213 回答