Help with mysql sub-query

Discussion in 'MySQL' started by gntombel, Apr 6, 2006.

  1. #1
    I have this query:

    update table t set t.status = 23
    where t.status = 5 AND t.ID IN( SELECT ID
    FROM items i
    group by i.ID
    having max(i.dateCreated) < CURDATE() - INTERVAL 14 DAY );

    The query does work properly with mysql 5. How can I change it so that it runs in version 4.0.x of mysql?
     
    gntombel, Apr 6, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What's the error that MySQL spits out?
     
    exam, Apr 6, 2006 IP
  3. gntombel

    gntombel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 'SELECT threadID
     
    gntombel, Apr 6, 2006 IP
  4. sacx13

    sacx13 Active Member

    Messages:
    438
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #4
    mysql 4 cannot accept subqueries ... so ... you need to rewrite your query to use temporary tables ... or any other way ...


    Regards
     
    sacx13, Apr 6, 2006 IP
  5. gntombel

    gntombel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How can I do that?
     
    gntombel, Apr 6, 2006 IP
  6. sacx13

    sacx13 Active Member

    Messages:
    438
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #6
    You need to do 2 querys.

    first SELECT ID FROM items i group by i.ID having max(i.dateCreated) < CURDATE() - INTERVAL 14 DAY ;

    second: update table t set t.status = 23 where t.status = 5 AND t.ID IN #RESULTFIRST#

    Or try this:

    select @id2=id FROM items i group by i.ID having max(i.dateCreated) < CURDATE() - INTERVAL 14 DAY ;update table t set t.status = 23 where t.status = 5 AND t.ID IN @id2;

    Regards
     
    sacx13, Apr 9, 2006 IP