Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MySQL - Update a table from same table using SELECT

MySQL - Update a table from same table using SELECT

Scheduled Pinned Locked Moved Database
announcementdatabasemysqlcomsysadmin
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I am trying to update the customers MySQL table using SELECT from the same table but getting:

    [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(destination_table.title) =

    here is the SQL statement:

    UPDATE customers destination_table

    SET (destination_table.title) =
    (

    SELECT source_table.title

    FROM customers source_table

    WHERE source_table.customer_id = 18

    )

    WHERE destination_table.customer_id = 14;

    Thanks, Jassim[^]

    Technology News @ www.JassimRahma.com

    M L B 3 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I am trying to update the customers MySQL table using SELECT from the same table but getting:

      [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(destination_table.title) =

      here is the SQL statement:

      UPDATE customers destination_table

      SET (destination_table.title) =
      (

      SELECT source_table.title

      FROM customers source_table

      WHERE source_table.customer_id = 18

      )

      WHERE destination_table.customer_id = 14;

      Thanks, Jassim[^]

      Technology News @ www.JassimRahma.com

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Not sure about MySQL but you cannot use an alias in an update statement, try removing destination_table and just using customers

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, I am trying to update the customers MySQL table using SELECT from the same table but getting:

        [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(destination_table.title) =

        here is the SQL statement:

        UPDATE customers destination_table

        SET (destination_table.title) =
        (

        SELECT source_table.title

        FROM customers source_table

        WHERE source_table.customer_id = 18

        )

        WHERE destination_table.customer_id = 14;

        Thanks, Jassim[^]

        Technology News @ www.JassimRahma.com

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        a.) (destination_table.title) should not be in brackets b.) Alias in Update, Looks strange but works :) (at least MSSQL)

        UPDATE t
        SET t.name= (SELECT s.name FROM CPTest1 s WHERE s.id = 1)
        FROM CPTest1 t
        WHERE t.id = '4'

        J 1 Reply Last reply
        0
        • L Lost User

          a.) (destination_table.title) should not be in brackets b.) Alias in Update, Looks strange but works :) (at least MSSQL)

          UPDATE t
          SET t.name= (SELECT s.name FROM CPTest1 s WHERE s.id = 1)
          FROM CPTest1 t
          WHERE t.id = '4'

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          UPDATE source_table
          SET source_table.title = (SELECT destination_table.title FROM customers destination_table WHERE destination_table.customer_id = 18)
          FROM customers source_table
          WHERE source_table.customer_id = 14

          [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM customers source_table
          WHERE source_table.customer_id = 14' at line 21

          Technology News @ www.JassimRahma.com

          L 1 Reply Last reply
          0
          • J Jassim Rahma

            UPDATE source_table
            SET source_table.title = (SELECT destination_table.title FROM customers destination_table WHERE destination_table.customer_id = 18)
            FROM customers source_table
            WHERE source_table.customer_id = 14

            [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM customers source_table
            WHERE source_table.customer_id = 14' at line 21

            Technology News @ www.JassimRahma.com

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thank you for your feedback, maybe I Need to install MySQL to check. Either way I would go as Mycroft Holmes suggested not to use an alias for the "UPDATE" itself.

            UPDATE customers
            SET customers.title = (SELECT destination_table.title FROM customers destination_table WHERE destination_table.customer_id = 18)
            WHERE customers.customer_id = 14

            [Edit] I just made a test on "SQL Fiddle". Without Alias I get also an error: "You can't specify target table 'CPTest1' for update in FROM clause" :( [Edit1] Finally I found here mysql update from select - same table - Stack Overflow[^] the answer

            update CPTest1 as t,
            (
            select name from CPTest1 where CPTest1.id = 1) as s
            set t.name = s.name
            where t.id = 4

            Tested on SQL Fiddle[^], MySQL 5.6 :)

            1 Reply Last reply
            0
            • J Jassim Rahma

              Hi, I am trying to update the customers MySQL table using SELECT from the same table but getting:

              [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(destination_table.title) =

              here is the SQL statement:

              UPDATE customers destination_table

              SET (destination_table.title) =
              (

              SELECT source_table.title

              FROM customers source_table

              WHERE source_table.customer_id = 18

              )

              WHERE destination_table.customer_id = 14;

              Thanks, Jassim[^]

              Technology News @ www.JassimRahma.com

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              Jassim Rahma wrote:

              UPDATE customers destination_table

              Do you see the un-escaped blank between customers and destination_table? That looks so wrong.

              M 1 Reply Last reply
              0
              • B Bernhard Hiller

                Jassim Rahma wrote:

                UPDATE customers destination_table

                Do you see the un-escaped blank between customers and destination_table? That looks so wrong.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                I do believe he is giving customers table and alias of destination_table, apparently MySQL allows aliasing in an update. This was my first reaction as well.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups