SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: JOIN in UPDATE

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    JOIN in UPDATE

    Code:
    myTable2
    (id) o
    (1) 2 
    (2) 3 
    (3) 2 
    (4) 5 
    
    myTabl3
    (id) cate
    (1) 1 
    (2) 2 
    (3) 1 
    (4) 1
    I have 2 tables like the above.
    The code1 below which has JOIN produces the result2 below.
    Code:
    code1
    
    
    SELECT myTable2.id, o
    FROM myTable2
    LEFT JOIN myTable3 ON myTable2.id=myTable3.id
    
    WHERE
    myTable3.cate=1
    
    ORDER BY myTable2.id
    
    result1
    (1) 2
    (3) 2
    (4) 5
    In order to use the JOIN in UPDATE, I made the following would-be code which actually causes a SQL syntax error.
    Code:
    would-be code
    UPDATE myTable2.
    LEFT JOIN myTable3 ON myTable2.id=myTable3.id
    
    SET o = 1
    
    WHERE
    myTable3.cate=1
    
    [b]target myTable2 data after updating [b]
    (1) 1
    (2) 3 
    (3) 1
    (4) 1
    How can I make it work correclty without the SQL syntax error?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    are you going to make us guess what the syntax error is?

    would it have anything to do with that period stuck on the end of the table name?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, the period was the problem.
    Shame on me...

  4. #4
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this code:

    UPDATE myTable2
    SET o = 1
    WHERE id in (select id from myTable3 where myTable3.cate=1)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •