从mysql表中获取最新添加的条目

时间:2017-08-26 06:13:07

标签: php mysql

我想从表中获取最新条目(added_on),其中client_id和financial_year是相同的,但我没有客户端ID或甚至财务年度比较我需要在表中比较它们自己

enter image description here

我使用了以下查询:

SELECT * 
  FROM file_mgmt fm
     , client c
     , cupboard cb 
  where fm.client_id = c.client_id 
   AND fm.cupboard_id = cb.cupboard_id
   AND fm.status = 'Active' 
 GROUP 
    BY fm.client_id
     , fm.financial_year

我得到了这个输出,这对每个client_id和financial_year都是唯一的,但它不是最新的条目

enter image description here

请帮助获取最新条目。

4 个答案:

答案 0 :(得分:1)

希望这会有所帮助

  SELECT * FROM file_mgmt fm, client c, cupboard cb where fm.`client_id` = c.`client_id` AND fm.`cupboard_id` = cb.`cupboard_id` AND fm.status = 'Active' 
    GROUP BY fm.`client_id`, fm.`financial_year`
    ORDER BY id DESC
    LIMIT 1

答案 1 :(得分:0)

当您使用group时,您必须先使用内部查询,然后才能使用它

SELECT fm.*,c.*,cb.* FROM file_mgmt fm, client c, cupboard cb 
JOIN (SELECT file_mgmt_id,client_id, financial_year,max(added_on) 
FROM file_mgmt GROUP BY client_id,financial_year) as fm1 
ON fm.file_mgmt_id = fm1.file_mgmt_id
where fm.`client_id` = c.`client_id` AND 
fm.`cupboard_id` = cb.`cupboard_id` AND fm.status = 'Active'

修改

尝试这个更简单的查询

   SELECT fm.*,max(added_on) FROM file_mgmt fm, 
   client c , cupboard cb 
   where fm.client_id = c.client_id 
   AND fm.cupboard_id = cb.cupboard_id
   AND fm.status = 'Active'  
   group by client_id,financial_year ;

答案 2 :(得分:0)

尝试添加

ORDER BY added_on DESC
LIMIT 1

在您的查询结束时。

答案 3 :(得分:0)

public static T ReadValue<T>(this IntPtr ptr, int length = 0)
    {
        Type valT = typeof(T);
        byte[] buffer = length > 0 ? new byte[length] : new byte[Marshal.SizeOf(valT)];
        IntPtr retBytes;
        object result;

        if (!ReadProcessMemory(CurrentProcessHandle, ptr, buffer, buffer.Length, out retBytes))
            return default(T);

        Console.WriteLine(ptr);

        if (valT == typeof(byte))
            result = buffer[0];
        else if (valT == typeof(bool))
            result = buffer[0] > 0;
        else if (valT == typeof(char))
            result = BitConverter.ToChar(buffer, 0);
        else if (valT == typeof(double))
            result = BitConverter.ToDouble(buffer, 0);
        else if (valT == typeof(float))
            result = BitConverter.ToSingle(buffer, 0);
        else if (valT == typeof(int))
            result = BitConverter.ToInt32(buffer, 0);
        else if (valT == typeof(long))
            result = BitConverter.ToInt64(buffer, 0);
        else if (valT == typeof(object))
            result = buffer;
        else if (valT == typeof(short))
            result = BitConverter.ToInt16(buffer, 0);
        else if (valT == typeof(string))
            result = Encoding.Default.GetString(buffer);
        else if (valT == typeof(uint))
            result = BitConverter.ToUInt32(buffer, 0);
        else if (valT == typeof(ulong))
            result = BitConverter.ToUInt64(buffer, 0);
        else if (valT == typeof(ushort))
            result = BitConverter.ToUInt16(buffer, 0);
        else
        {
            IntPtr newVal = Marshal.AllocHGlobal(buffer.Length);
            Marshal.Copy(buffer, 0, newVal, buffer.Length);
            result = newVal;
            Marshal.FreeHGlobal(newVal);
        }

        return (T) result;
    }