How to make a conditional insert

Discussion in 'MySQL' started by dragonclaws, Mar 6, 2011.

  1. #1
    Hi,
    Here's my table structure:
    id : Primary Key (auto increment)
    user: Userid (varchar(35)
    pageid: page id (varchar(35))
    time: timestamp

    Here's what i want to do:
    I want to make an insert query which inserts only if the difference between the current timestamp and the max timestamp for the combination user,pageid is more than 30.
    Generally i can handle it on php side by making a select query first and then doing a conditional check there itself and then making the insert query but i wish t odo it in only one query. Is it possible?

    i tried something like this:
    insert into user_action(user,pageid,time) select 'suyash',10,1299359394 from user_action where not exists ( select * from user_action where user='suyash' and pageid=10 and time > 1299359364 order by time desc limit 1 )
    but its not working properly. m really desperate here... please help!
    Thanks in advance!!
     
    dragonclaws, Mar 6, 2011 IP
  2. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this
    insert into user_action(user,pageid,time) 
    select user, pageid, time 
    from user_action 
    where user='suyash' and pageid=10 and time > 1299359364 order by time desc limit 1
    PHP:
     
    jkl6, Mar 8, 2011 IP