A sort of IF THEN ELSE in SQL

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default A sort of IF THEN ELSE in SQL

    Hi All

    I know this isn't an SQL syntax NG per say (esp MySQL), but there are far
    more legible gurus in here than the MySQL groups so I hope you will bear
    with me.

    All I want to know is if there is a sort of IF ... THEN ... ELSE command in
    MySQL? The reason being that if a product code exists then I want to do an
    update, but if it doesn't then I want to do an insert.

    At the moment, I'm having to do a Select query first to see if it does exist
    and then do a 2nd update/insert query depending on the result I get from the
    1st.

    I'd like to sort this into one query if poss so your feedback would be
    appreciated.

    Rgds

    Laphan


    Laphan Guest

  2. Similar Questions and Discussions

    1. Sort
      Hi, I want to write a perl script to do something like this Abc 12.8 8 "left" 1 15.7 Def 13.8 9 "top" 0 19.7
    2. [ANN] Sort::Key 0.02
      Hi, I have released Sort::Key 0.02, a module for sorting objects by some key. It's really fast, usually much faster than perl core sort...
    3. Sort bug
      It appears that the runtime cannot sort a datagrid column when the items in the column contain a NaN or Infinity. Is there a way to override the...
    4. memory sort and disk sort
      I check the sysprofile table and find there are 700 times disk sort, I think it is lack of sort memory. I want to turn all the disk sort into the...
    5. Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
  3. #2

    Default Re: A sort of IF THEN ELSE in SQL

    > At the moment, I'm having to do a Select query first to see if it does
    exist
    > and then do a 2nd update/insert query depending on the result I get from
    the
    > 1st.
    You're going to have to do that anyway, I think. In SQL Server, I do this:

    IF EXISTS (SELECT 1 FROM tbl WHERE <logic to determine uniqueness>)
    UPDATE tbl ... WHERE <logic to determine uniqueness>
    ELSE
    INSERT ...

    A roughly performance-equivalent alternative is:

    UPDATE tbl ... WHERE <logic to determine uniqueness>
    IF @@ROWCOUNT = 0
    INSERT ...

    For MySQL specifics (e.g. whether the EXISTS clause exists, or something
    similar), you'll probably have better luck in a MySQL newsgroup or from the
    MySQL docs.

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)


    Aaron [SQL Server MVP] Guest

  4. #3

    Default Re: A sort of IF THEN ELSE in SQL

    Thanks for the feedback Aaron

    Much appreciated.

    Rgds

    Laphan


    Aaron [SQL Server MVP] <ten.xoc@dnartreb.noraa> wrote in message
    news:e0ECx$IWEHA.2696@TK2MSFTNGP09.phx.gbl...
    > At the moment, I'm having to do a Select query first to see if it does
    exist
    > and then do a 2nd update/insert query depending on the result I get from
    the
    > 1st.
    You're going to have to do that anyway, I think. In SQL Server, I do this:

    IF EXISTS (SELECT 1 FROM tbl WHERE <logic to determine uniqueness>)
    UPDATE tbl ... WHERE <logic to determine uniqueness>
    ELSE
    INSERT ...

    A roughly performance-equivalent alternative is:

    UPDATE tbl ... WHERE <logic to determine uniqueness>
    IF @@ROWCOUNT = 0
    INSERT ...

    For MySQL specifics (e.g. whether the EXISTS clause exists, or something
    similar), you'll probably have better luck in a MySQL newsgroup or from the
    MySQL docs.

    --
    [url]http://www.aspfaq.com/[/url]
    (Reverse address to reply.)




    Laphan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139