-2

I am new to MySQL. I would like to ask how to pass the result of a select statement to a variable. The variable will then be used in succeeding queries (CASE WHEN or IF ELSE).

sample query:

select count(*) from table;
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Dave
  • 37
  • 7
  • 2
    Possible duplicate of [Declare a variable in MySQL](http://stackoverflow.com/questions/11754781/declare-a-variable-in-mysql) – philipvr Dec 07 '15 at 20:05

1 Answers1

1

Can use user-defined variables, e.g.:

select count(*) into @itemCount from table;
DBug
  • 2,502
  • 1
  • 12
  • 25
  • It worked! Thanks! My dilemma now is how to use the value inside [at]itemcount in IF ELSE statement or CASE WHEN statement. i.e. [at]itemCount < 1, it will insert new row, if not, select a particular column in the table. – Dave Dec 07 '15 at 21:15
  • Can use it anywhere you can use a value. `IF @itemCount < 1 THEN INSERT ... ELSE UPDATE .... END IF;` `CASE @itemCount WHEN 0 THEN .... ELSE .... END` – DBug Dec 07 '15 at 22:04