Mysql基于日期获取行

时间:2016-10-15 06:59:59

标签: mysql

以下是代码:

SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          ip.dblItemPAmount, MAX(ip.dtmItemPasOf) AS EXR 
FROM TBLREQUESTDETAILS RD,tblitem I,tblvendor v,tblitemunit iu,tblitemprice ip` 
WHERE RD.strReqDItemCode = I.strItemCode 
AND RD.strReqDItemUnitCode = iu.strItemUnitCode 
AND RD.strReqDVendCode = v.strVendCode 
AND i.strItemCode = ip.strItemPItemCode 
and RD.strReqDReqHCode = 'RQST121' 
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName, ip.dblItemPAmount
ORDER BY EXR desc ;

AND这是结果: enter image description here

如果我想获取每个itemname,vendorname和itemunit的当前价格,我该怎么办?我想只获取价格最新的那些行...请帮我,那些有盒子的行是我想要获取的行

1 个答案:

答案 0 :(得分:1)

您可以在分组值中使用where(并使用explicict join notatio)

SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          ip.dblItemPAmount, ip.dtmItemPasOf AS EXR 
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode 
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode 
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode 
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode 
WHERE RD.strReqDReqHCode = 'RQST121' 
and ( RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName, ip.dtmItemPasOf) 

    in ( SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          MAX(ip.dtmItemPasOf) 
        FROM TBLREQUESTDETAILS RD
        INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode 
        INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode 
        INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode 
        INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode 
        WHERE RD.strReqDReqHCode = 'RQST121'  
        GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName 
    )
 ORDER BY EXR desc ;

(并使用explicict join notation ..我认为更具可读性)