How can i set multiple values in one variable in mysql?

When you need to retrieve a single row from a table or query, you can use the following syntax in SQL Server:

   DECLARE @name VARCHAR(30);
   SELECT @name = city FROM cities;

But what happens if SELECT returns multiple rows?

Assume we have the following table definition and data:

  CREATE TABLE cities (city VARCHAR(30));
 
  INSERT INTO cities VALUES ('St. Louis');
  INSERT INTO cities VALUES ('San Diego');
  INSERT INTO cities VALUES ('Seattle');

Let's run SELECT and output data:

   DECLARE @name VARCHAR(30);
   SELECT @name = city FROM cities;
   PRINT @name;
 
   -- Prints Seattle

SQL Server does not raise an error, and returns the last value. It defines how many rows meet the search criteria, and you can obtain this number using @@ROWCOUNT:

   DECLARE @name VARCHAR(30);
   SELECT @name = city FROM cities;
   PRINT @@ROWCOUNT;
 
   -- Prints 3

Note that when you want to retrieve one row, but multiple rows may meet the search condition, you have to check @@ROWCOUNT > 0, not @@ROWCOUNT = 1 to define if a row exists:

   DECLARE @name VARCHAR(30);
   SELECT @name = city FROM cities;
 
   IF @@ROWCOUNT = 1
    PRINT @name;
 
   -- Nothing is printed as @@ROWCOUNT is equal to 3
 
   SELECT @name = city FROM cities;
 
   IF @@ROWCOUNT > 0
    PRINT @name;
 
   -- Prints Seattle

Since SQL Server tries to find all rows (to calculate @@ROWCOUNT) while you just want to find the first row, consider using TOP 1 in the query:

   DECLARE @name VARCHAR(30);
   SELECT TOP 1 @name = city FROM cities;
   PRINT @name;
 
   -- Prints St. Louis

When you use TOP 1 you can safely check @@ROWCOUNT = 1 to define if a row was found.

Migration Resources

 Summary: in this tutorial, you will learn how to develop stored procedures that return multiple values.

MySQL stored function returns only one value. To develop stored programs that return multiple values, you need to use stored procedures with INOUT or OUT parameters.

If you are not familiar with INOUT or OUT parameters, check it out the stored procedure’s parameters tutorial for the detailed information.

Let’s take a look at the orders table in the sample database.

How can i set multiple values in one variable in mysql?

The following stored procedure accepts customer number and returns the total number of orders that were shipped, canceled, resolved, and disputed.

DELIMITER $$ CREATE PROCEDURE get_order_by_cust( IN cust_no INT, OUT shipped INT, OUT canceled INT, OUT resolved INT, OUT disputed INT) BEGIN -- shipped SELECT count(*) INTO shipped FROM orders WHERE customerNumber = cust_no AND status = 'Shipped'; -- canceled SELECT count(*) INTO canceled FROM orders WHERE customerNumber = cust_no AND status = 'Canceled'; -- resolved SELECT count(*) INTO resolved FROM orders WHERE customerNumber = cust_no AND status = 'Resolved'; -- disputed SELECT count(*) INTO disputed FROM orders WHERE customerNumber = cust_no AND status = 'Disputed'; END

Code language: SQL (Structured Query Language) (sql)

In addition to the IN parameter, the stored procedure takes four additional OUT parameters: shipped, canceled, resolved, and disputed. Inside the stored procedure, you use a SELECT statement with the COUNT function to get the corresponding total of orders based on the order’s status and assign it to the respective parameter.

To use the get_order_by_cust stored procedure, you pass customer number and four user-defined variables to get the out values.

After executing the stored procedure, you use the SELECT statement to output the variable values.

CALL get_order_by_cust(141,@shipped,@canceled,@resolved,@disputed); SELECT @shipped,@canceled,@resolved,@disputed;

Code language: SQL (Structured Query Language) (sql)
How can i set multiple values in one variable in mysql?

Calling stored procedures that return multiple values from PHP

The following code snippet shows you how to call the stored procedure that returns multiple values from PHP.

<?php /** * Call stored procedure that return multiple values * @param $customerNumber */ function call_sp($customerNumber) { try { $pdo = new PDO("mysql:host=localhost;dbname=classicmodels", 'root', ''); // execute the stored procedure $sql = 'CALL get_order_by_cust(:no,@shipped,@canceled,@resolved,@disputed)'; $stmt = $pdo->prepare($sql); $stmt->bindParam(':no', $customerNumber, PDO::PARAM_INT); $stmt->execute(); $stmt->closeCursor(); // execute the second query to get values from OUT parameter $r = $pdo->query("SELECT @shipped,@canceled,@resolved,@disputed") ->fetch(PDO::FETCH_ASSOC); if ($r) { printf('Shipped: %d, Canceled: %d, Resolved: %d, Disputed: %d', $r['@shipped'], $r['@canceled'], $r['@resolved'], $r['@disputed']); } } catch (PDOException $pe) { die("Error occurred:" . $pe->getMessage()); } } call_sp(141);

Code language: PHP (php)

The user-defined variables, which are preceded by the @ sign, are associated with the database connection, therefore, they are available for access between the calls.

In this tutorial, we have shown you how to develop a stored procedure that returns multiple values and how to call it from PHP.

Was this tutorial helpful?

How do I assign multiple values to a variable in MySQL?

SET @a := "20100630"; SELECT * FROM wordbase WHERE verified = @a; But it does not work when there are multiple values stored in a variable. SET @a := "'20100630', '20100701' "; SELECT * FROM wordbase WHERE verified in (@a);

How can I get multiple values in one column in MySQL?

In this case, we use GROUP_CONCAT function to concatenate multiple rows into one column. GROUP_CONCAT concatenates all non-null values in a group and returns them as a single string. If you want to avoid duplicates, you can also add DISTINCT in your query.

How can I add multiple values in one cell in MySQL?

INSERT INTO LOGI (related_id) VALUES('281,283,284,285,286'); However this breaks the whole foreign key paradigm. You won't be able to run SELECT queries and join tables based on this column. Better to create a cross-reference table.

How can I store multiple values in one row in MySQL?

MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause.