Custom variable in MySQL subquery

Query Output:
> 

SELECT
  @foo := 1,
  @foo,
  (SELECT @foo),
  (SELECT foo FROM (SELECT @foo AS foo) subselect)

+ -------------- + --------- + ------------------ + ----------------------------------------------------- +
| @foo := 1      | @foo      | (SELECT @foo)      | (SELECT foo FROM (SELECT @foo AS foo) subselect)      |
+ -------------- + --------- + ------------------ + ----------------------------------------------------- +
| 1              | 1         | 1                  | 0                                                     |
+ -------------- + --------- + ------------------ + ----------------------------------------------------- +
1 rows

      

Well ... I just want to know why the fourth column value is 0 and not 1.

0


source to share


1 answer


Because this one FROM (SELECT @foo AS foo)

is evaluated before that @foo := 1

. Basically anything in FROM will be evaluated before your SELECT.



It should actually be null, but I am assuming you assigned your session variable to be null somewhere else.

+2


source







All Articles