开发者

MySQL: Working with @vars and subqueries

开发者 https://www.devze.com 2023-01-28 23:24 出处:网络
I think MySQL is the most difficult language for me, I\'ve started to learn MySQL one year ago and I still don\'t understand what I can do and how I can build my queries without asking here.

I think MySQL is the most difficult language for me, I've started to learn MySQL one year ago and I still don't understand what I can do and how I can build my queries without asking here.

I have a simple query where I'd like to see how I can @vars, so I've tryied this:

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

the result is:

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]

Dosen't it expected @param to 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 per result of a specific table which can have multiple results, so I use subqueries but I don't know how it 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 are:

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

Can someone explain me how @vars can be passed correctly to subqueries?

When subqeries should be used?

Basically, what is the process of a query, does exist some clear guide on how data is returned?


It does not seems required a user defined variables for this query

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

精彩评论

暂无评论...
验证码 换一张
取 消