MySQL SELECT,存储在变量中

时间:2010-05-04 19:06:39

标签: mysql stored-procedures select local-variables

对于存储过程,我想做一个SELECT,并将列的值存储到变量中。

我该怎么做?

我想做这样的事情:

    DECLARE countTemp INT;
    SET countTemp=(SELECT COUNT(Name) FROM mytable WHERE Name= var_name LIMIT 0,1);

OR,像这样:

    DECLARE countTemp INT;
    SELECT countTemp=ColumnXYZ FROM mytable WHERE Name= var_name LIMIT 0,1;

但是,我试过这些,MySQL说我的语法不正确;我该怎么办?

2 个答案:

答案 0 :(得分:2)

像这样:

DECLARE myvar nvarchar(50);

SELECT ATextColumn INTO myvar FROM myTable LIMIT 1,1;

SELECT CONCAT('myvar is ',myvar ,' .');

http://www.java2s.com/Code/SQL/Procedure-Function/UseselectintotoassignvaluetoanIntegervariable.htm

答案 1 :(得分:0)

您可以在选择查询中轻松设置变量

SELECT @countTemp := ColumnXYZ FROM mytable WHERE Name= var_name LIMIT 0,1;

@countTemp 是您的变量!