Home > Database > Mysql Tutorial > body text

Why Am I Getting an \'Undeclared Variable\' Error with SELECT INTO in MySQL?

Linda Hamilton
Release: 2024-11-01 04:15:28
Original
559 people have browsed it

Why Am I Getting an

SELECT INTO and the "Undeclared Variable" Error

When attempting to execute a query that uses the SELECT INTO syntax to insert data into a variable as follows:

SELECT id_subscriber
  INTO newsletter_to_send
  FROM subscribers 
Copy after login

An error "1327 - Undeclared variable: newsletter_to_send" might be encountered. This error occurs because MySQL does not support SELECT INTO for variables.

Solution

To insert data into a table using the result of a query, the INSERT ... SELECT syntax should be used instead:

INSERT INTO newsletter_to_send
SELECT id_subscriber FROM subscribers 
Copy after login

This query will insert the id_subscriber values from the subscribers table into the newsletter_to_send table.

Additional Considerations

When using INSERT ... SELECT, consider including a WHERE clause to filter the data being inserted. Additionally, ensure that the table specified in the INTO clause already exists before executing the query.

The above is the detailed content of Why Am I Getting an \'Undeclared Variable\' Error with SELECT INTO in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!