For example, I have a query:

SELECT id, subid, alias FROM table WHERE id IN ( 2, 8, 4, 7 ) ORDER by id 

I need to get every line found and save it to a temporary variable in order to compare it with the next found sample line like @result = IF( @old.subid == @new.id , true, false) . How to implement it in a mysql query?

    1 answer 1

     SELECT id, subid, alias, @result:=IF(subid=@oldid, true, false), @oldid:=id FROM table, (select @oldid:=0,@result:=NULL) A WHERE id IN ( 2, 8, 4, 7 ) ORDER by id 

    In the from subquery, initialization with initial values ​​is required. In the sample list of the main query, the storage of oldid should be performed after working with this value from the previous record, i.e. at the end of the list of output values.

    • alias subquery would be necessary - splash58
    • @ splash58 Right, forgot - Mike
    • Damn does not work .... - modelfak
    • @modelfak What does it mean "does not work" what it does not do and what it does instead - Mike
    • @Mike oldid - always the current record ID, not the previous one. I understand it is impossible to refer to the previous entry in the sample. - modelfak