如何在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;
当前回答
我想在这里给出我的答案,这样人们就可以尝试一下,我认为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 );
其他回答
对于任何使用concat_ws函数中的@variable来获得连接值的人,不要忘记用空值重新初始化它。否则,它可以对同一会话使用旧值。
Set @Ids = '';
select
@Ids := concat_ws(',',@Ids,tbl.Id),
tbl.Col1,
...
from mytable tbl;
声明: SET @a = 1; 用法: INSERT INTO ' t ' (' c ') VALUES (@a);
设置值
declare @Regione int;
set @Regione=(select id from users
where id=1) ;
select @Regione ;
我想在这里给出我的答案,这样人们就可以尝试一下,我认为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 );
使用set或select
SET @counter := 100;
SELECT @variable_name := value;
例子:
SELECT @price := MAX(product.price)
FROM product