How to fix errors when using mysql select into?

When using the SELECT INTO statement, MySQL will throw the following error:

Variable_name is not declared: ERROR 1327 (42000)

The reason for this error is that, in MySQL, the SELECT INTO statement is not used to insert the result set into a variable. The correct syntax is to use the SELECT … INTO statement to insert the result into a table.

When you want to store the query result in a variable, you can use the SELECT … INTO structure instead of SELECT INTO.

Here is an example of the correct usage:

SELECT column_name1, column_name2 INTO @variable1, @variable2 FROM table_name WHERE condition;

You need to modify column_name1, column_name2, @variable1, @variable2, table_name, and condition based on your actual situation.

If you only want to store the query results in a temporary table instead of a variable, you can use the CREATE TEMPORARY TABLE statement to create a temporary table and use the INSERT INTO … SELECT statement to insert the query results into the temporary table.

I hope this can help you solve the problem.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds