Mysql 内部连接只是最后的结果

Mysql 内部连接只是最后的结果,mysql,Mysql,我只需编辑我的旧代码,并尝试创建漂亮的复杂查询 我的查询如下所示: SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country FROM axnmrs_case

我只需编辑我的旧代码,并尝试创建漂亮的复杂查询

我的查询如下所示:

SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country
                    FROM axnmrs_cases
                    INNER JOIN axnmrs_calculations ON ( axnmrs_cases.case_id = axnmrs_calculations.case_id
                    AND axnmrs_cases.country = axnmrs_calculations.country ) 
                    WHERE vin = :vin
这工作和显示结果,什么是完美的(即使我不能相信它:D),但我只需要最后的计算,而不是所有的

                INNER JOIN axnmrs_calculations ON ( axnmrs_cases.case_id = axnmrs_calculations.case_id
                AND axnmrs_cases.country = axnmrs_calculations.country ) ORDER BY axnmrs_calculations.calculation_id DESC LIMIT 1
但是我不知道如何限制只是内部连接而不是整个查询,有人能告诉我吗


谢谢

如果您想要每个案例id和国家/地区的最后一次计算,那么您需要创建一个派生表(from子句中的子查询)来获取这些数据,并将案例和计算表加入到这个派生表中

您需要在计算表中具有自动增量id或时间戳,或者具有标识计算的最后版本的标志。我假设计算表中有一个自动增量id字段:

SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country
FROM axnmrs_cases
INNER JOIN axnmrs_calculations AS ac ON ( axnmrs_cases.case_id = ac.case_id AND axnmrs_cases.country = ac.country )
INNER JOIN (SELECT MAX(id) AS maxid, case_id, country FROM axnmrs_calculations GROUP BY case_id, country) AS T
ON ac.id=T.maxid and ac.case_idóT.case_id and ac.country=T.country
WHERE vin = :vin

如果希望每个case_id和country进行最后一次计算,则需要创建一个派生表(from子句中的子查询)来获取这些数据,并将cases和calculations表连接到此派生表上

您需要在计算表中具有自动增量id或时间戳,或者具有标识计算的最后版本的标志。我假设计算表中有一个自动增量id字段:

SELECT axnmrs_cases.claimnumber as claim, axnmrs_cases.vin as vin, axnmrs_cases.date_created as date, axnmrs_calculations.totalcosts as totalcosts, axnmrs_cases.country as country
FROM axnmrs_cases
INNER JOIN axnmrs_calculations AS ac ON ( axnmrs_cases.case_id = ac.case_id AND axnmrs_cases.country = ac.country )
INNER JOIN (SELECT MAX(id) AS maxid, case_id, country FROM axnmrs_calculations GROUP BY case_id, country) AS T
ON ac.id=T.maxid and ac.case_idóT.case_id and ac.country=T.country
WHERE vin = :vin

您想获取每个案例的最后一次计算吗?嘿@Shadow,对不起,我没有指定,您是对的,我想@Shadow不正确,因为您在(axnmrs\u cases.case\u id=…和axnmrs\u cases.country)上有
。但是我可能错了。你想得到每个案例id的最后一次计算吗?嘿@Shadow,很抱歉我没有指定,你是对的,我想@Shadow是不正确的,因为你在(axnmrs\u cases.case\u id=…和axnmrs\u cases.country)
上有
。但我可能错了。