

Mysql> select *from getLastRecord ORDER BY id DESC LIMIT 1 To get the last record, the following is the query.

Our lats record is with id 4 and Name ‘Carol’. Mysql> insert into getLastRecord values(4,'Carol') ĭisplay all records with the help of select statement. Mysql> insert into getLastRecord values(3,'Johnson') Mysql> insert into getLastRecord values(2,'Ramit') Mysql> insert into getLastRecord values(1,'John') Let us first create a table and insert some records with the help of insert command.Īfter creating the above table, we will insert records with the help of insert command. If you search criteria for the current and previous rows are the same (except the relative ages check of course) then using LAG() should be far more efficient as you don't need either of the other two references to the table.To select the last row, we can use ORDER BY clause with desc (descending) property and Limit 1. If you know there will always be at least one previous row for each row found in "current" the join for "older" can be an inner join which may allow the query runner to be more efficient. The null check on the middle take in the where clause instead of the join clause means that any combination where there is a more recent row than the one matched by the "older" join, so you know "current" is the current row and "older" is the most recent row before that which matches the "previous row" criteria. Whether the sub query or extra join option is more efficient than the other will depend on your DBMS's query planner and your search criteria, so I suggest you try both with realistic test data to benchmark them. These will only work if the current row and the one you consider the previous row are find using the same filtering clauses though, otherwise you and up needing to reference the table three times: first to find the base row(s) you are interested in, next joined to that to find previous rows to consider, and third either a left outer join or correlated sub query referencing both to rule out extra rows if there may be several that match the "pervious row" criteria. This would be simpler with the window function lag(), but your version does not seem to support that according to the discussion under the duplicate post.Īssuming you have a recent enough version, infomix supports the LAG and LEAD functions over ranking operations, see This way you do not need GROUP BY, because only one (or no) row is left-joined.Īlso assuming deal_dt and cancel_dt are supposed to come from the same cancelled deal. LEFT JOIN product_shipping C ON C.product_id = P.product_idĪssuming (product_id,cancel_dt) to be UNIQUE, else you need tiebreaker criteria to be unambiguous. I suggest you join to previous products that match criteria and (if any exist) narrow down to the one where no later row exists: SELECT P.product_id AS product_id, LEFT JOIN product_shipping R ON (C.product_id = R.product_id and TO_CHAR(R.cancel_reason_no) MATCHES ('2', '4') and R.cancel_dt <= C.deal_dt)
#Mysql join latest record how to
WHERE TO_CHAR(C.deal_reason_no ) in ('1', '3', '5')ĪND C.deal_dt between ' 00:00:00' and ' 00:00:00'īut I'm not sure how to join to get latest previous record.Įdit: Add my idea. I think the SQL could be like this: SELECT I want to query the product_id and deal_dt whose deal_reason_no is 1, 3 or 5 and deal_dt is between ' 00:00:00' and ' 00:00:00' and the latest previous deal_dt and latest previous cancel_dt whose cancel_reason_no is 2 or 4 for the product_id. If the cancelled product is bought by other customer then the shipping is recorded in the new row, and product_name can be changed in the new shipping. Once cancelled cancel_dt and cancel_reason_no will be set. However product_id is not unique in the table since the every product shipping can be cancelled. Once the shipping date is arranged, the deal_dt and deal_reason_no will be set and cancel_dt and cancel_reason_no is null.

I have a table whose schema is like this: CREATE TABLE product_shipping(
