Thread: Query Help

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17

    Query Help


    I have a table with a foreign key X and another column, Y.

    If there is only ONE row with the foreign key X, and the column Y in that row is a certain value, I want to update column Y to a different value.

    Can this be done with one query?
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,690
    Rep Power
    4288
    Originally Posted by Arty Zifferelli
    Can this be done with one query?
    including a check to confirm that there's only one row?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    192
    Rep Power
    17
    including a check to confirm that there's only one row?
    Well, yes. I don't think it's possible, but I'm not an SQL guru, so I thought I'd ask...
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,690
    Rep Power
    4288
    "You cannot update a table and select from the same table in a subquery." -- https://dev.mysql.com/doc/refman/5.7/en/update.html
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo