MySQL - IF EXISTS

Discussion in 'MySQL' started by lektrikpuke, Mar 26, 2009.

  1. #1
    Hi y'all,

    IF (SELECT COUNT(*) FROM tbl_name WHERE category_code ='some-category-code') THEN UPDATE tbl_name SET active='0' WHERE category_code = 'some-category-code' END IF

    Why won't the above work? :confused: I know the update part works by itself. The select count part also works by itself.

    Thanks for any help.

    Rob :cool:
     
    lektrikpuke, Mar 26, 2009 IP
  2. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #2
    Maybe I'm asking the wrong question. I need to be able to update a row if row exists. I say this because if I blindly try to update a row and it doesn't exist, it stops the batch cold.

    Ideas? =)
     
    lektrikpuke, Mar 26, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You probably need to use a stored procedure or use several queries in your actual script (assuming this is being run from a website).
     
    jestep, Mar 27, 2009 IP
  4. Gangsta

    Gangsta Active Member

    Messages:
    145
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    you may try to use REPLACE
     
    Gangsta, Mar 28, 2009 IP
  5. Siteapps

    Siteapps Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    UPDATE table SET active = IF(category_code ='some-category-code', 0, active);
     
    Siteapps, Mar 28, 2009 IP
  6. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #6
    Thanks for the replies. This UPDATE table SET active = IF(category_code ='some-category-code', 0, active); isn't quite what I'm looking for as it will set a category active if it's not "the" category (which may have already been set to inactive). Works great if it is the category, just that little side effect.

    Thanks again. :D

    Rob
     
    lektrikpuke, Mar 31, 2009 IP
  7. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you copied the query exactly, then the most likely culprit is that you are not setting the if qualifier. Try:

    IF (SELECT COUNT(*) FROM tbl_name WHERE category_code ='some-category-code') > 0 THEN UPDATE tbl_name SET active='0' WHERE category_code = 'some-category-code' END IF

    (notice the > 0 part that was added)

    The post title says if exists, but you are just using an if statement, not an if exists. If you want to use the exists function, then the query should look like:

    IF EXISTS(SELECT * FROM tbl_name WHERE category_code ='some-category-code') THEN UPDATE tbl_name SET active='0' WHERE category_code = 'some-category-code' END IF

    (notice the count function has been removed)

    The reason you would remove the count function is that count will always returns a value so it will always exist.
     
    druidelder, Apr 1, 2009 IP
  8. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #8
    Sorry about the misleading post title. I'd tried many different approaches including if exists, but could get none to work. Obviously I wasn't thinking clearly (read as forgot to actually proofread my post). Thanks, I will try both suggestions.

    Rob :D
     
    lektrikpuke, Apr 1, 2009 IP
  9. David26

    David26 Well-Known Member

    Messages:
    1,304
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    140
    #9
    Simple, without knowing what you are trying to do, something like this would work:

     
    David26, Apr 2, 2009 IP
  10. girish.gb

    girish.gb Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    IF EXISTS(SELECT * FROM tblusers WHERE LoginName = 'superadmin') THEN
    SET @a = 1;
    ELSE
    SET @a = 2;
    END IF;
    SELECT @a

    Hi I am New to MySql was working on MSSQL Can any one help me in whats wrong in the above code. The same code will work in MSSQL.

    Thanks in Advance
    Girish
     
    girish.gb, Dec 10, 2011 IP