SELECT INTO and “Undeclared variable” error

前端 未结 10 2136
你的背包
你的背包 2020-11-30 06:58

When I try to execute following query:

SELECT id_subscriber
  INTO newsletter_to_send
  FROM subscribers 

I get an error:

相关标签:
10条回答
  • 2020-11-30 07:39

    MySQL does not support SELECT INTO [table]. It only supports SELECT INTO [variable] and can only do this one variable at a time.

    What you can do, however is use the CREATE TABLE syntax with a SELECT like so:

    0 讨论(0)
  • 2020-11-30 07:43

    If you want to create a new table as a Duplicate table.

    CREATE TABLE WorkerClone LIKE Worker;   //only structure will show no data
    CREATE TABLE WorkerClone Select * from  Worker; //DUPLICATE table created with all details.
    
    0 讨论(0)
  • 2020-11-30 07:49

    MySQL does not support the SELECT ... INTO ... syntax.

    You have to use the INSERT INTO ... SELECT .. syntax to accomplish there.

    Read more here.. http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

    0 讨论(0)
  • 2020-11-30 07:53
    INSERT ... SELECT
    

    http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

    INSERT INTO newsletter_to_send
    SELECT id_subscriber FROM subscribers 
    

    PS: are you sure you don't need in WHERE clause?

    0 讨论(0)
  • 2020-11-30 07:57

    mysql don't support SELECT ... INTO ... syntax,

    if it's a new table, use CREATE TABLE ... SELECT ... syntax.

    example:

    CREATE TABLE artists_and_works
      SELECT artist.name, COUNT(work.artist_id) AS number_of_works
      FROM artist LEFT JOIN work ON artist.id = work.artist_id
      GROUP BY artist.id;
    

    read more here create-table-select

    0 讨论(0)
  • 2020-11-30 07:58

    I tried working on this just now and this works for me:

    CREATE TABLE New_Table_name
    SELECT * FROM Original_Table_name;
    
    0 讨论(0)
提交回复
热议问题