As we can see in the solution:
Expression are evaluated in the column order in the select
clause.
This is the same for the where clause. An advice is to test
first the expression that get more chance to return false at the
head of the where conditions :
- SET @BRAND=0;
- SET @ct=0;
- SELECT
- *
- FROM
- (SELECT
- IF(@BRAND<>id_BRAND,@ct:=0 ,@ct:=@ct ) ,
- @ct:=@ct+1 AS ct,
- @BRAND:=id_BRAND,
- t2.*
- FROM
- (SELECT
- st.ID_BRAND,
- st.ID_MODEL,
- SUM(COMPTE) AS total,
- AVG(totalm)
- …