insert with nested select problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • faif
    New Member
    • Sep 2007
    • 7

    insert with nested select problem

    I want to implement a query similar to this in mysql:


    [CODE=mysql]
    insert into table1 (field1)
    (select t2.field2
    from table2 t2
    where t2.field3="stri ng" and t2.field4=table 1.field4)
    [/CODE]

    the reference to table1 from the inner query (table1.field4) does not work.

    Is there a way to achieve this?

    Thank you.
    Last edited by mwasif; Sep 20 '07, 12:44 PM. Reason: Replaced [CODE] with [CODE=mysql]
  • pradeep kaltari
    Recognized Expert New Member
    • May 2007
    • 102

    #2
    Originally posted by faif
    I want to implement a query similar to this in mysql:


    Code:
    insert into table1 (field1)
      (select t2.field2
      from table2 t2
      where t2.field3="string" and t2.field4=table1.field4)
    the reference to table1 from the inner query (table1.field4) does not work.

    Is there a way to achieve this?

    Thank you.
    Hi,
    In the above query first the SELECT statement is executed and then the result set is inserted in the TABLE1.

    So, it is clear that the "table1" should also be included in the Select statement. The query would look something like:
    [code=mysql]
    insert into table1 (field1)
    (select t2.field2
    from table2 t2, table1
    where t2.field3="stri ng" and t2.field4=table 1.field4)
    [/code]

    I hope this solves your problem.

    - Pradeep
    Last edited by mwasif; Sep 20 '07, 12:49 PM. Reason: Replaced [CODE] with [CODE=mysql]

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Hi faif and pradeep kaltari,
      Kindly use appropriate CODE tags when posting source code.

      Comment

      • faif
        New Member
        • Sep 2007
        • 7

        #4
        Originally posted by pradeep kaltari
        Hi,
        In the above query first the SELECT statement is executed and then the result set is inserted in the TABLE1.

        So, it is clear that the "table1" should also be included in the Select statement. The query would look something like:
        [code=mysql]
        insert into table1 (field1)
        (select t2.field2
        from table2 t2, table1
        where t2.field3="stri ng" and t2.field4=table 1.field4)
        [/code]

        I hope this solves your problem.

        - Pradeep
        Thank you for the answer, but unfortunately I've already tried this and it is not what I'm looking for.

        I want to copy the values of the fields of table2 to the fields
        of table1. I use the t2.field4=table 1.field4 because I want to copy the contents in the appropriate records, and don't create new. When I'm using a copy of table1
        in the inner query, new records are created in table1, and this doesn't solve my problem.

        Comment

        • amitpatel66
          Recognized Expert Top Contributor
          • Mar 2007
          • 2358

          #5
          Originally posted by faif
          Thank you for the answer, but unfortunately I've already tried this and it is not what I'm looking for.

          I want to copy the values of the fields of table2 to the fields
          of table1. I use the t2.field4=table 1.field4 because I want to copy the contents in the appropriate records, and don't create new. When I'm using a copy of table1
          in the inner query, new records are created in table1, and this doesn't solve my problem.
          Then you need to use UPDATE insted of INSERT.
          Try below code:
          [code=sql]
          UPDATE table1 SET field1 = (SELECT t2.field2 from table2 t2 WHERE t2.field3="stri ng" AND t2.field4=table 1.field4)
          [/code]

          Comment

          • faif
            New Member
            • Sep 2007
            • 7

            #6
            Originally posted by amitpatel66
            Then you need to use UPDATE insted of INSERT.
            Try below code:
            [code=sql]
            UPDATE table1 SET field1 = (SELECT t2.field2 from table2 t2 WHERE t2.field3="stri ng" AND t2.field4=table 1.field4)
            [/code]
            Thank you. I tried but mysql doesn't like it... :(
            The error message is:

            #1064 - You have an error in your SQL syntax near 'SELECT t2.field3
            FROM table2 t2
            WHERE t2.field' on line 2

            Info: The inner query returns multiple rows.

            Comment

            • faif
              New Member
              • Sep 2007
              • 7

              #7
              Finally, a two-step solution has been found. The solution is:

              [code=mysql]
              CREATE TEMPORARY TABLE temp
              (select t2.field2, t2.field3
              from table2 t2
              where t2.field3="stri ng");

              update table1 t1, temp t
              set t1.field1=t.fie ld3
              where t1.field2=t.fie ld2;
              [/code]

              Thank you all for the help.

              Comment

              Working...