如何在mysql中声明一个变量,让我的第二个查询可以使用它?
我想这样写:
SET start = 1;
SET finish = 10;
SELECT * FROM places WHERE place BETWEEN start AND finish;
如何在mysql中声明一个变量,让我的第二个查询可以使用它?
我想这样写:
SET start = 1;
SET finish = 10;
SELECT * FROM places WHERE place BETWEEN start AND finish;
当前回答
设置值
declare @Regione int;
set @Regione=(select id from users
where id=1) ;
select @Regione ;
其他回答
不同类型的变量:
局部变量(没有@前缀)是强类型的,范围限定在声明它们的存储程序块中。请注意,如DECLARE语法中所述:
DECLARE只允许在BEGIN…END复合语句,必须在其开头,在任何其他语句之前。
用户变量(前缀为@)类型松散,范围仅限于会话。注意,它们既不需要声明,也不能声明——直接使用即可。
因此,如果你在定义一个存储程序,并且确实需要一个“局部变量”,你将需要删除@字符,并确保你的DECLARE语句位于程序块的开头。否则,要使用“用户变量”,请删除DECLARE语句。
此外,你需要把你的查询用圆括号括起来,以便作为子查询执行:
SET @countTotal = (SELECT COUNT(*) FROM nGrams);
或者,你可以使用SELECT…成:
SELECT COUNT(*) INTO @countTotal FROM nGrams
对于任何使用concat_ws函数中的@variable来获得连接值的人,不要忘记用空值重新初始化它。否则,它可以对同一会话使用旧值。
Set @Ids = '';
select
@Ids := concat_ws(',',@Ids,tbl.Id),
tbl.Col1,
...
from mytable tbl;
我想在这里给出我的答案,这样人们就可以尝试一下,我认为MySql的解决方案更容易理解:
set @countVal = (select count(*) from STATION);
/**
499/2 = 249,5 -> 250 -- ceil
499/2 = 249,5 + 1 = 250,5 -- floor 250
500/2 = 250 -- ceil 250
= 250 + 1 = 251 -- flor 251
**/
set @ceilVal = ceil(@countVal/2);
set @floorVal = floor( (@countVal/2) + 1);
SELECT ROUND(AVG( latitude ),4) FROM
(SELECT @lineNum:= @lineNum + 1 as id,
lat_n as latitude
FROM STATION s, ( SELECT @lineNum :=0 ) pivot
ORDER BY lat_n) as a
WHERE id IN ( @ceilVal, @floorVal );
设置值
declare @Regione int;
set @Regione=(select id from users
where id=1) ;
select @Regione ;
MySQL中主要有三种类型的变量:
User-defined variables (prefixed with @): You can access any user-defined variable without declaring it or initializing it. If you refer to a variable that has not been initialized, it has a value of NULL and a type of string. SELECT @var_any_var_name You can initialize a variable using SET or SELECT statement: SET @start = 1, @finish = 10; or SELECT @start := 1, @finish := 10; SELECT * FROM places WHERE place BETWEEN @start AND @finish; User variables can be assigned a value from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or NULL value. User-defined variables are session-specific. That is, a user variable defined by one client cannot be seen or used by other clients. They can be used in SELECT queries using Advanced MySQL user variable techniques. Local Variables (no prefix) : Local variables needs to be declared using DECLARE before accessing it. They can be used as local variables and the input parameters inside a stored procedure: DELIMITER // CREATE PROCEDURE sp_test(var1 INT) BEGIN DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10; SELECT var1, start, finish; SELECT * FROM places WHERE place BETWEEN start AND finish; END; // DELIMITER ; CALL sp_test(5); If the DEFAULT clause is missing, the initial value is NULL. The scope of a local variable is the BEGIN ... END block within which it is declared. Server System Variables (prefixed with @@): The MySQL server maintains many system variables configured to a default value. They can be of type GLOBAL, SESSION or BOTH. Global variables affect the overall operation of the server whereas session variables affect its operation for individual client connections. To see the current values used by a running server, use the SHOW VARIABLES statement or SELECT @@var_name. SHOW VARIABLES LIKE '%wait_timeout%'; SELECT @@sort_buffer_size; They can be set at server startup using options on the command line or in an option file. Most of them can be changed dynamically while the server is running using SET GLOBAL or SET SESSION: -- Syntax to Set value to a Global variable: SET GLOBAL sort_buffer_size=1000000; SET @@global.sort_buffer_size=1000000; -- Syntax to Set value to a Session variable: SET sort_buffer_size=1000000; SET SESSION sort_buffer_size=1000000; SET @@sort_buffer_size=1000000; SET @@local.sort_buffer_size=10000;