Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:49:18+00:00 2026-05-30T03:49:18+00:00

With LEFT JOIN SET @foo:=0; SELECT @foo FROM test t1 LEFT JOIN test t2

  • 0

With LEFT JOIN

SET @foo:=0;
SELECT @foo 
FROM test t1 
  LEFT JOIN test t2 ON t2.id=t1.id AND (@foo:=@foo+1);

prints 0,0,0,0,0,0,0…

but with JOIN

SET @foo:=0;
SELECT @foo 
FROM test t1 
   JOIN test t2 ON t2.id=t1.id AND (@foo:=@foo+1);

prints 1,2,3,4,5,6…

Anybody knows? Thanks!

UPD. This queries produce the same rows except @foo value

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:49:20+00:00Added an answer on May 30, 2026 at 3:49 am

    Assignments for session variables don’t work inside ON clause.

    The reason you see @foo incremented for INNER JOIN is that optimizer runs your query as:

    SET @foo:=0;
    SELECT @foo 
    FROM test t1 
       JOIN test t2 
       WHERE t2.id=t1.id AND (@foo:=@foo+1);
    

    Using EXPLAIN SELECT... you should see using where in extra column.

    You can also use EXPLAIN EXTENDED SELECT... and then SHOW WARNINGS to see how exactly optimizer runs your query.

    In your case the LEFT JOIN query is run exactly as it is written, as for this type of join you can get different results for the same condition in ON and WHERE.

    If you want to increment @foo for LEFT JOIN use:

    SET @foo:=0;
    SELECT @foo 
    FROM test t1 
      LEFT JOIN test t2 ON t2.id=t1.id
      WHERE @foo:=@foo+1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this equivalent to a LEFT JOIN ? SELECT DISTINCT a.name, b.name FROM tableA
I have a query like this: SELECT TaFellesVaktjournal.*, TaBygg.ByggNavn FROM TaFellesVaktjournal LEFT JOIN TaBygg
SELECT bar.*, baz.foo FROM bar LEFT JOIN baz if I have a column called
I have this query SELECT products_list.id_cat_unique, products_categories_list.*, COUNT(products_list.id_cat_unique) as counter FROM products_categories_ids LEFT JOIN
SELECT * FROM tbl_group_join tgj LEFT JOIN tbl_groups tg ON tg.group_id = tgj.group_id LEFT
mysql> select a.id,a.parent from qa a left join qa b on a.parent=b.parent where b.id=7;
This question follows on from MYSQL join results set wiped results during IN ()
SELECT * FROM Table_A LEFT JOIN Table_B ON (Table_A.A_ID = Table_B.A_ID) INNER JOIN Table_C
how to make this select statement more faster? the first left join with the
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.