1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple SQL Update Query Not Easy!

Discussion in 'MySQL' started by Aitch, Apr 5, 2015.

  1. #1
    Hi folks, I wonder if someone could help me please.

    phpMyAdmin is not somewhere I'm overly familiar with, but I have tried to work out how to run a simple SQL Query. I can't see where the problem is, but there must be one because I keep getting errors.

    The Query

    I have a property website that has many properties at various prices. I would like to disable (not delete) all live properties under a certain rental price range. Here's what I know:

    1. The table name is property
    2. The column for price is named pricerent
    3. The column for live property is available

    So this is what I have written:

    Update (table name) set (status) to (No) where the rent is less than (amount). So my query looks like this:

    Update property set available='No' where pricerent<200

    And the error I get is"

    #1064 - 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 'Update property set available='No' where pricerent<200 LIMIT 0, 30' at line 2

    I would really love to know where I'm going wrong here, not least because this is a function that will be very useful as it will save having to disable properties one by one in the CMS admin panel.

    Thank you in advance for any help on this.

    Aitch
     
    Solved! View solution.
    Aitch, Apr 5, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm assuming you've checked that "available" i set to a varchar or text - ie, it's not a boolean (depending on either being 0 or 1)?
    Apart from that, you should be good to go... "UPDATE `property` SET `available` = 'No' WHERE `pricerent`< '200'";
    You might wanna try to enclose the 200 in single quotes - depending on what type of column it is.
     
    PoPSiCLe, Apr 6, 2015 IP
  3. Aitch

    Aitch Well-Known Member

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Hi PoPSiCLe. Thanks very much for responding to my post. You lost me a bit on the: a varchar or text - ie, it's not a boolean (depending on either being 0 or 1)? - said it was alien to me :oops: I tried to enclose the 200 in single quotes though, but still got errors. I've attached a screen print. Maybe you can see from that what the problem is?

    [​IMG]
    Thanks in advance,

    Aitch
     
    Aitch, Apr 6, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Could you have a look at the structure of the table, and check the type of each column? The type will be something like VARCHAR, TEXT, INT etc
     
    PoPSiCLe, Apr 6, 2015 IP
  5. Aitch

    Aitch Well-Known Member

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Hi PoPSiCLe, thanks for getting back ;)

    OK, I just took a look. There are many types in the table so I looked at the ones that I am trying to write the query for (I hope that's right?). This is what I see:
    • pricerent = char(20) utf8_general_ci
    • available = varchar(200) utf8_general_ci
    Aitch
     
    Aitch, Apr 6, 2015 IP
  6. #6
    Hm, okay. In that case, the query should work. There is no reason it should fail, really...
    Oh, wait. Are you running it like it shows in the picture? Remove the line with "select" that query isn't closed, and those two will fail as they're set now
     
    PoPSiCLe, Apr 6, 2015 IP
  7. Aitch

    Aitch Well-Known Member

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Hi PoPSiCLe,

    Thanks once again. You solved a big headache for me today and the query ran through just fine a few moments ago ;). The down side is that it didn't disable the available properties under a certain price range on the live site, so I will have to try and work out why that is next. But for now, it's good to know where I was going wrong with the query, so at least I can tick that box as done :)

    BTW XXX, I may have to undo that query now so I guess that's another something to learn. However, the site seems to be functioning just fine, so perhaps it's better to just leave well alone if nothing appears to be broken.

    Anyway, thanks once again for your input, you've been a great help

    Aitch
     
    Aitch, Apr 6, 2015 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Yeah... You dont have an "undo" function in mysql/phpmyadmin. That is a entirely different approach, and usually needs a programmatical approach. That being said, if this didn't affect the showing on the webpage, that might be because there are other tables involved in asscerning availability. Since I don't know the website's querystructure, nor the database schema, it's impossible to tell what solution might work.
     
    PoPSiCLe, Apr 6, 2015 IP
  9. Aitch

    Aitch Well-Known Member

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #9
    Hi PoPSiCLe,

    Yes, I already read that there is no undo feature for queries. That said, surely running a reverse query would undo things, but I don't say that with any knowledge or confidence - yet! Even so, I've learned some stuff today that I didn't know yesterday - thanks to you for your help - so I will progress further and perhaps be a little less hasty to run stuff until I know exactly what's what ;)

    All the best to you ;)

    Aitch
     
    Aitch, Apr 6, 2015 IP
  10. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #10
    Yes, a "reverse query" MIGHT work, but you need to remember that it might backfire. Say you have a table with a boolean column (on/off) - in this table you have several values on (1) and several values off (0) - if you change this column to 1 based on a WHERE column = 0 ALL of them will be 1, which will make a reversal impossible, because you'll have nothing to base the reversal on.
    This is why large databases often have several failsafes, like a history-table, a column or table for values being changed etc.
    Basically, a reversal mostly only works if your WHERE-condition has more than one comparison, or you have some kind of distinct selector which is not part of the actual values changed (so you can compare on this again)
     
    PoPSiCLe, Apr 6, 2015 IP
  11. Aitch

    Aitch Well-Known Member

    Messages:
    113
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #11
    Thanks PoPSiCLe,

    Lesson #2. Don't mess around in phpMyAdmin until I know "exactly" what I'm doing ;) I'm going to take time out to learn about this stuff now because would like to manage and maintain my own databases under the hood, so to speak.

    Have a great day.

    Aitch
     
    Aitch, Apr 6, 2015 IP
  12. liquidone

    liquidone Member

    Messages:
    67
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    43
    #12
    Just as an FYI I like writing queries in HeidiSQL it's a bit friendlier then phpMyAdmin. It works nicely with MySQL and is free to download.
     
    liquidone, Apr 6, 2015 IP