Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How mysql assigns query results to variables

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how mysql assigns query results to variables. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Mysql assigns query results to variables: 1, use "DECLARE variable name type [DEFAULT default];" statement to create variables; 2, use "SELECT field name INTO variable name FROM table name WEHRE query condition;" statement assign query results to variables.

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Mysql's method of assigning query results to variables

1. Create variables

Variables can be defined using the DECLARE keyword in MySQL, and the basic syntax is as follows:

DECLARE variable name type [DEFAULT default]

If the DEFAULT clause is omitted, the default value is NULL.

Example: define the variable my_sql, and the data type is INT

DECLARE my_sql INT

2. Assign the query result to the variable

You need to use the SELECT..INTO statement, and its basic syntax is as follows:

SELECT col_name [...] INTO var_name [,...] FROM table_name WEHRE condition

Where:

The col_name parameter represents the field name of the query

The var_name parameter is the name of the variable

The table_name parameter refers to the name of the table

The condition parameter refers to the query condition.

Note: when assigning a query result to a variable, the query statement can only return a single row.

Example: query a record with id 2 from the tb_student table and assign the id value of that record to the variable my_sql. The SQL statement is as follows:

SELECT id INTO my_sql FROM tb_student WEHRE id=2; above is how mysql assigns query results to variables. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report