चर के साथ अंतिम दृष्टिकोण उचित है। आप यह भी कोशिश कर सकते हैं:
SELECT collect.*,
(select max(timestamp)
from data
where data.channel_id = collect.channel_id AND data.timestamp < collect.timestamp
) AS prev_timestamp
FROM data AS collect
WHERE collect.channel_id = 14 AND collect.timestamp >= 0
ORDER BY collect.timestamp;
इसके अलावा, इस पर इंडेक्स बनाएं:कलेक्ट (चैनल_आईडी, टाइमस्टैम्प)।
संपादित करें:
निम्नलिखित सबसे तेज़ हो सकते हैं:
select d.*,
if(@channel_id = channel_id, @prev_timestamp, NULL) as prev_timestamp,
@channel_id := channel_id, @prev_timestamp = timestamp
from data d cross join
(select @channel_id := 0, @prev_timestamp := 0) vars
where collect.channel_id = 14 AND collect.timestamp >= 0
order by channel_id, timestamp;