MySQL infinite loop in a subquery? -
the following query results infinite loop:
select *, (select t2.`value` `table` t2 t2.`variable` = 'xxx' , t2.`read` = (select max(t1.`read`) `table` t1 t1.`variable` = 'xxx' , unix_timestamp(t1.`read`) < (1401801648 - n.integers) ) ) (select @n:=@n + 1 integers mysql.help_relation, (select @n:=0) dum limit 48) n
i need result 48 rows 48 different time ranges (in example 1401801648
minus {1..48}
). each row should contain value depending on current time range. query on bottom these 48 ranges. query in middle needed find date newest entry older calculated timestamp (1401801648 - n.integers)
. upper query tells me value of row date query in middle.
when "n.integers" replaced number works fine.
without subquery (t2) query not in loop(?):
select *, (select max(t1.`read`) `table` t1 t1.`variable` = 'xxx' , unix_timestamp(t1.`read`) < (1401801648 - n.integers) ) (select @n:=@n + 1 integers mysql.help_relation, (select @n:=0) dum limit 48) n
an alternative method avoiding using variables:-
select sub1.a_cnt, t2.value table t2 inner join ( select sub1.a_timestamp, sub1.a_cnt, t1.variable, max(t1.read) max_timestamp ( select (1401801648 - units.i + 10 * tens.i) a_cnt,(1401801648 - units.i + 10 * tens.i) a_timestamp (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) units, (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) tens units.i + 10 * tens.i between 1 , 48 ) sub1 inner join table t1 , unix_timestamp(t1.read) < sub1.a_timestamp t1.variable = 'xxx' group sub1.a_timestamp ) sub2 on t2.read = sub2.max_timestamp , t2.variable = sub2.variable
this uses load of unioned queries getting constants generate numbers 0 9, cross joins against copy of , minor calulation numbers 0 99, clause narrow down range 1 48, , uses calculate timestamps required.
this joined against table max read date each timestamp / generated number.
the results of joined against table other details row (in case value column).
not tested gives idea.
Comments
Post a Comment