Professional Web Applications Themes

Simple IF, ELSE in SQL - MySQL

I'm new to MySQL and am have trouble with conditionals (IF/ELSE) Trying the simplest possible query: IF EXISTS (SELECT 1) SELECT 4 ELSE SELECT 1; IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; ENDIF; IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; END IF; (I execute these separately, just trying to show some permutations I thought of) or any variation I can think of involving IF and ELSE. Using actual data and tables yields the same results. ("You have an error in you MySQL syntax" - ...

  1. #1

    Default Simple IF, ELSE in SQL

    I'm new to MySQL and am have trouble with conditionals (IF/ELSE)

    Trying the simplest possible query:

    IF EXISTS (SELECT 1) SELECT 4 ELSE SELECT 1;
    IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1;
    IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; ENDIF;
    IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; END IF;
    (I execute these separately, just trying to show some permutations I
    thought of)

    or any variation I can think of involving IF and ELSE. Using actual
    data and tables yields the same results. ("You have an error in you
    MySQL syntax" - helpful)

    I'm using MySQL 5.0.19 (same syntax works in SQL 2000)

    Thanks for any help!

    Lee Guest

  2. #2

    Default Re: Simple IF, ELSE in SQL

    Lee wrote:
    > I'm new to MySQL and am have trouble with conditionals (IF/ELSE)
    >
    > Trying the simplest possible query:
    >
    > IF EXISTS (SELECT 1) SELECT 4 ELSE SELECT 1;
    > IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1;
    > IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; ENDIF;
    > IF EXISTS (SELECT 1) THEN SELECT 4 ELSE SELECT 1; END IF;
    > (I execute these separately, just trying to show some permutations I
    > thought of)
    >
    > or any variation I can think of involving IF and ELSE. Using actual
    > data and tables yields the same results. ("You have an error in you
    > MySQL syntax" - helpful)
    >
    > I'm using MySQL 5.0.19 (same syntax works in SQL 2000)
    >
    > Thanks for any help!
    >
    You can use IF statements only inside stored routines.

    [url]http://dev.mysql.com/doc/refman/5.0/en/if-statement.html[/url]

    delimiter //
    create procedure test1()
    begin
    IF EXISTS (SELECT 1) THEN
    SELECT 4;
    ELSE
    SELECT 1;
    END IF;
    end //
    delimiter ;

    call test1();
    +---+
    | 4 |
    +---+
    | 4 |
    +---+

    This article covers stored routines in MySQL and the differences between MySQL and other SQL dialects.
    [url]http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.html[/url]

    ciao
    gmax

    --
    _ _ _ _
    (_|| | |(_|>< The Data Charmer
    _|
    [url]http://datacharmer.blogspot.com/[/url]
    Giuseppe Maxia Guest

  3. #3

    Default Re: Simple IF, ELSE in SQL

    Thanks, I finally figured that out too.. Someone posted a comment on
    the MySQL reference to that effect. What a pain. Thanks for the
    article.

    Lee Guest

Similar Threads

  1. Need Simple Answer to Simple Contribute/Firefox question
    By Adobe nickname in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: February 13th, 06:59 PM
  2. Simple one
    By spdorsey webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 1
    Last Post: November 6th, 12:48 AM
  3. I am sure this is very simple!
    By Steve Podbury in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 23rd, 03:56 PM
  4. A simple problem with a really simple form
    By Simon Harvey in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 13th, 04:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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