0

My table is like this::

+-------+-----------------+---+
 id     |  created             |
+-------+-----------------+---+
| 2sdv  | 2016-05-18 14:08:14 |
| 2sdv  | 2016-05-18 14:25:22 |
| 2sdv  | 2016-05-18 14:26:01 |
| 2sdv  | 2016-05-19 07:19:13 |
+-------+---------------------+

Lets say this id ='2sdv' created on these dates I want the ouput to be like this

+-------+-----------------+---+------+-----------------+
 id     |  created            | new_date
+-------+-----------------+---+------+-----------------+
| 2sdv  | 2016-05-18 14:08:14 | 2016-05-18 14:25:22    |
| 2sdv  | 2016-05-18 14:25:22 | 2016-05-18 14:26:01    |
| 2sdv  | 2016-05-18 14:26:01 | 2016-05-19 07:19:13    |
| 2sdv  | 2016-05-19 07:19:13 |
+-------+---------------------+------+-----------------+

In Oracle we can use lead() function but I want my query to be in MySQL. how can I get next value of created date column in a new column in multiple rows fetch .

6
  • your question is not very clear. Commented Jun 3, 2016 at 6:54
  • You want the minimum y date greater than the corresponding x date. Because you also want the latest x date, this would be a LEFT JOIN Commented Jun 3, 2016 at 6:58
  • your id's would be unique or same?? Commented Jun 3, 2016 at 7:00
  • In this particular case my id is same but i want to add a new column having next value of date created Commented Jun 3, 2016 at 7:03
  • This is not something MySQL is designed to be able to do. You should solve this problem outside MySQL. It's easily solved in a language like PHP for example Commented Jun 3, 2016 at 7:18

1 Answer 1

1

As per my comment above...

SELECT x.*
     , MIN(y.created) new_date -- the minimum y date...
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON y.id = x.id 
   AND y.created > x.created -- ... greater than the corresponding x date
 GROUP 
    BY x.id
     , x.created;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.