Search code examples
phpmysqlsqlphpmyadminmamp

SQL server : stored procedure


I have a table with 6 columns (id, deviceID, athleteName, fieldName, valueTx, dateTime).

So far when I call my function getSensors it display all rows.

However I want to display at each update of the table only the latest row, depending on dateTime and deviceID columns.

I've never used store procedure, so I'm wondering if is it the best to do ? If not what do you suggest me to do ?

Many thanks in advance!!!


Solution

  • You don't need a stored procedure for this. A simple query:

    select t.*
    from t
    where t.datetime = (select max(t2.datetime) from t t2 where t2.deviceID = t.deviceID);
    

    If you are given a deviceID, you can get the most recent record as:

    select t.*
    from t
    where t.deviceID = v_deviceID
    order by t.datetime desc
    limit 1;