Results 1 to 4 of 4

Thread: If-then-else

  1. #1
    Join Date
    Sep 2006
    Posts
    2

    Unanswered: If-then-else

    I am new to MySQL and am ploughing through.
    A question I am sure everyone else can answer without trying:

    How do I code an SQL statement to do this (if I can?):

    Select the required account

    IF the account is found

    IF the balance is less than $100
    DO y
    ELSE
    DO z
    ENDIF

    ENDDO

    And how would this interact with my website/PHP? Checking '$result' would show mw what? Or would I code 2 seperate SQL statements from my PHP script?

    Hope I am not showing myself to be too ignorant here!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    when you select the account from the table, you can use a CASE expression to return something if the balance is < 100, and something else if the balance is >= 100

    however, if you want to do something, then this will have to be done with php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2006
    Posts
    2
    Thanks r937
    just to get this right:
    In my PHP script set up a query to check for the record's existence on the table
    If I find it, I do the checking in the PHP script on the balance
    Depending on the balance I perform the appropriate action - is that correct.

    What do I do if somebody else comes along and updates the balance while I am checking it - or is this beyond ths scope of this thread?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, i think you have to lock the record

    just what is this "appropriate action" that needs doing?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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