SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATE Column Only IF

    I have a script that updates a bunch of columns in a certain row. Most of the columns I want updated, no matter what their current content; however, I have a date column where I want to update the date only if the new date is later than the date already entered. Is there some way to get MySQL to do this for me, instead of selecting the existing date, having PHP check it, then updating the row? I'd rather avoid the extra call.
    Ed Rands

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try this:

    $sql="UPDATE table SET date = 'new_date' WHERE date < 'new_date';

    Note, that will update every record, so if you only want a certain record updated, then add another condition to the WHERE clause.
    Last edited by 7stud; May 28, 2001 at 13:58.

  3. #3
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I thought of that one, and may have to resort to it, but doing so would mean I need two UPDATE calls.

    I'm fairly certain this is invalid, but it should at least get the message across. What I really wanted to is something like this..

    UPDATE requests SET name='name', IF ('new_date' > dated) dated='new_date', summary='summary' WHERE ID='5';

    (Strange. I can't put the above line in the vB code tags. When I do, and click submit, all I get is a blank page, the source code of which is blank between the body tags.)
    Last edited by Percipient; May 28, 2001 at 14:09.
    Ed Rands

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's an IF with SELECT:
    http://www.mysql.com/doc/C/o/Control...functions.html

    You might want to try different permutations but I have a feeling you won't get it to work.

    Try joining the table to itself perhaps; but other than subqueries (which don't work in MySQL and may not work in this instance) I can't think of a way to do it.


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
  •