I am trying to write a query in PHP SQL, which has to select from DB 1 the value of each repeating number, I can’t cope with it:

enter image description here

You need to select in id_num every 2 in a row with num. In num, the score is not always the same, but it does not matter. The main thing is that the score is always greater than 5. For example, in the numbers 5 you need to choose id_num = 3 to 7, you need id_num = 5 to 8, this is id_num = 34 and so on.

    2 answers 2

    For mysql. We build a temporary field with the order in the num groups sorted in ascending order by id and select those lines in which this field corresponds to the second line

    select num, id_num from (SELECT id, id_num, @order:=IF(num=@snum, @order+1, 0) ord, @snum:=num num FROM t, (select @snum:=0,@order:=0) t1 ORDER by num, id) t2 where ord=1 

    Demo

    • It works, what you need) thank you0 - pwnz22
    • I am glad to help. Good luck! - splash58
    • Can you still tell me how to enumerate the string num in a new line like this - pwnz22
    • one
      instead of id - id_t_p - splash58
    • one
      where ord = 1 change to another condition - splash58

    2 options, or describe the problem more clearly.

      SELECT * FROM table WHERE id_num = 34 или SELECT * FROM table OrderBy num