How to store a query result in a variable in mysql

SET @v1 := SELECT COUNT(*) FROM user_rating;
SELECT @v1

When I execute this query with set variable this error is shown.

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'SELECT count(*) FROM user_rating' at line 1

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

(1 row(s) returned)
Execution Time : 00:00:00:343
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:343

asked Apr 9, 2012 at 7:48

How to store a query result in a variable in mysql

Query MasterQuery Master

6,8395 gold badges32 silver badges55 bronze badges

1

Surround that select with parentheses.

SET @v1 := (SELECT COUNT(*) FROM user_rating);
SELECT @v1;

answered Apr 9, 2012 at 7:50

Sergio TulentsevSergio Tulentsev

222k42 gold badges364 silver badges359 bronze badges

9

Additionally, if you want to set multiple variables at once by one query, you can use the other syntax for setting variables which goes like this: SELECT @varname:=value.

A practical example:

SELECT @total_count:=COUNT(*), @total_price:=SUM(quantity*price) FROM items ...

answered Apr 25, 2014 at 0:19

YirkhaYirkha

11.7k5 gold badges39 silver badges52 bronze badges

use this

 SELECT weight INTO @x FROM p_status where tcount='value' LIMIT 1;

tested and workes fine...

answered Apr 27, 2014 at 22:45

How to store a query result in a variable in mysql

Aman MauryaAman Maurya

1,31512 silver badges26 bronze badges

0

Select count(*) from table_name into @var1; 
Select @var1;

How to store a query result in a variable in mysql

AgeDeO

3,1342 gold badges22 silver badges57 bronze badges

answered Dec 17, 2019 at 10:37

0


To store query result in a variable with MySQL, use the SET command. The syntax is as follows −

SET @anyVariableName = ( yourQuery);

To understand the above concept, let us create a table. The following is the query to create a table −

mysql> create table QueryResultDemo
   −> (
   −> Price int
   −> );
Query OK, 0 rows affected (0.59 sec)

Now let us insert some records into the table. The following is the query to insert records −

mysql> insert into QueryResultDemo values(100);
Query OK, 1 row affected (0.17 sec)

mysql> insert into QueryResultDemo values(20);
Query OK, 1 row affected (0.13 sec)

mysql> insert into QueryResultDemo values(200);
Query OK, 1 row affected (0.10 sec)

mysql> insert into QueryResultDemo values(80);
Query OK, 1 row affected (0.15 sec)

Display all records from the table with the help of select statement. The query to display all records is as follows −

mysql> select *from QueryResultDemo;

The following is the output −

+-------+
| Price |
+-------+
|   100 |
|    20 |
|   200 |
|    80 |
+-------+
4 rows in set (0.00 sec)

Now you can set the query result in a variable with the help of SET command. The query is as follows.

mysql> Set @TotalPrice = (select sum(Price) from QueryResultDemo);
Query OK, 0 rows affected (0.00 sec)

Check what is the value stored in variable “TotalPrice”, using the SELECT statement −

mysql> select @TotalPrice;

The following is the output −

+-------------+
| @TotalPrice |
+-------------+
|         400 |
+-------------+
1 row in set (0.00 sec)

How to store a query result in a variable in mysql

Updated on 30-Jul-2019 22:30:24

  • Related Questions & Answers
  • How to store query result (a single document) into a variable?
  • Store a variable with the result of a MySQL SELECT CASE?
  • Set the result of a query to a variable in MySQL?
  • How to assign the result of a MySQL query into a variable?
  • Set user variable from result of query in MySQL?
  • How to declare a variable in MySQL for a normal query?
  • How to store MongoDB result in an array?
  • How do I split a numerical query result in MySQL?
  • How can I display MySQL query result vertically?
  • How can we use SET statement to assign a SELECT result to a MySQL user variable?
  • How to get file extension of file as a result of MySQL query?
  • Assign an SQL result to variable from prepared statement in MySQL?
  • MySQL query to return a string as a result of IF statement?
  • Store unicode in a char variable in Java
  • How to write a valid MySQL query and update with a custom variable?

Can we store query in variable in SQL?

Yup, this is possible of course.

Can we store value in variable in MySQL?

You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another. User variables are written as @ var_name , where the variable name var_name consists of alphanumeric characters, . , _ , and $ .

How do I store select query results in a table?

Exporting query results to a database.
To open the Export Query Results wizard, select File > Export. ... .
Select Database. ... .
From the Source list, select the query result set that you want to export. ... .
Specify the database where you want to save the query results data in the Database field..

How do I store select query results in variable in SQL Developer?

PL/SQL SELECT INTO examples.
First, declare a variable l_customer_name whose data type anchors to the name columns of the customers table. ... .
Second, use the SELECT INTO statement to select value from the name column and assign it to the l_customer_name variable..
Third, show the customer name using the dbms_output..