MySQL: Working with @vars and Subqueries

I think that MySQLis the most difficult language for me, I started learning MySQL a year ago, and I still do not understand what I can do, and how I can build my queries without asking here.

I have a simple query where I would like to see how I can @vars, so I tried to do this:

SELECT param_1, @param := param_2, @param
FROM my_table

result:

param_1    @param := param_2       @param
Dave       3                       [BLOB - 1B]
Mike       4                       [BLOB - 2B]
Luke       2                       [BLOB - 2B]
Bob        65                      [BLOB - 2B]
Dean       6                       [BLOB - 2B]

@paramWas expected to be equal to param_2?

param_1    @param := param_2       @param
Dave       3                       3
Mike       4                       4
Luke       2                       2
Bob        65                      65
Dean       6                       6

What is [BLOB - 1B]and [BLOB - 2B]?

Sometimes I make subqueries when I need to get only one value for the result of a specific table, which can have several results, so I use subqueries, but I donโ€™t know how this works exactly:

SELECT table_1.param_1, table_1.param_2, @param = table_1.param_3, new_table_2.param_1,     new_table_2.param_2
FROM table_1
LEFT JOIN (SELECT *
            FROM table_2
            WHERE new_table_2.param_1 = @param
            LIMIT 1
           ) new_table_2
ON new_table_2.param_1 = @param
WHERE table_1.param_2 = 33

My questions:

Why do I get [BLOB - 1B]and [BLOB - 2B]the first query?

- , @vars ?

?

, , ?

+3
1

,

SELECT 
  table_1.param_1, 
  table_1.param_2, 
  table_1.param_3, 
  new_table_2.param_1, 
  new_table_2.param_2
FROM table_1
LEFT JOIN 
(
  SELECT * FROM table_2 WHERE new_table_2.param_1 = table_1.param3 LIMIT 1
) as new_table_2
ON new_table_2.param_1 = table_1.param3
WHERE table_1.param_2 = 33;
0

Source: https://habr.com/ru/post/1778847/


All Articles