Professional Web Applications Themes

Trouble with a simple if statement. - MySQL

I am having trouble with what should be a simple IF() statement. SELECT IF ( (SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2') = 0, (INSERT INTO pp_ubooks (uid, book_id) VALUES (2,2)), (DELETE FROM pp_ubooks WHERE uid='2' AND book_id='2') ); Returns: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO pp_ubooks (uid, book_id) VALUES (2,2)), (DELETE FROM pp_ubooks WHER' at line 4 SELECT IF ( (SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2') = 0, (DELETE FROM pp_ubooks WHERE uid='2' ...

  1. #1

    Default Trouble with a simple if statement.

    I am having trouble with what should be a simple IF() statement.

    SELECT
    IF (
    (SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2')
    = 0,
    (INSERT INTO pp_ubooks (uid, book_id) VALUES (2,2)),
    (DELETE FROM pp_ubooks WHERE uid='2' AND book_id='2')
    );

    Returns:
    ERROR 1064 (42000): You have an error in your SQL syntax; check the
    manual that corresponds to your MySQL server version for the right
    syntax to use near 'INTO pp_ubooks (uid, book_id) VALUES (2,2)),
    (DELETE FROM pp_ubooks WHER' at line 4

    SELECT
    IF (
    (SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2')
    = 0,
    (DELETE FROM pp_ubooks WHERE uid='2' AND book_id='2'),
    (INSERT INTO pp_ubooks (uid, book_id) VALUES (2,2))
    );

    Returns:
    ERROR 1064 (42000): You have an error in your SQL syntax; check the
    manual that corresponds to your MySQL server version for the right
    syntax to use near 'DELETE FROM pp_ubooks WHERE uid='2' AND
    book_id='2'),
    (INSERT INTO pp_ub' at line 4

    Yet...
    SELECT
    IF (
    (SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2')
    = 0,
    0,
    1
    );

    Returns:
    +----------------------------------------------------------------------------+
    | IF((SELECT COUNT(*) FROM pp_ubooks WHERE uid='2' AND book_id='2') =
    0,0,1) |
    +----------------------------------------------------------------------------+
    |
    1 |
    +----------------------------------------------------------------------------+
    1 row in set (0.00 sec)


    What could possibly be going wrong here?

    It must be an error in my syntax, but according to what I read in the
    manual, it should work just fine.

    Any input would be appreciated.

    Daz

    Daz Guest

  2. #2

    Default Re: Trouble with a simple if statement.

    Daz wrote:
     
    > 0,0,1) |
    > +----------------------------------------------------------------------------+ 
    > 1 |
    > +----------------------------------------------------------------------------+
    > 1 row in set (0.00 sec)
    >
    >
    > What could possibly be going wrong here?
    >
    > It must be an error in my syntax, but according to what I read in the
    > manual, it should work just fine.
    >
    > Any input would be appreciated.
    >
    > Daz[/ref]

    I could be wrong, but I don't think you can do a "DELETE FROM" or an "INSERT INTO"
    inside a "SELECT" statement.

    --

    Murdoc Guest

  3. #3

    Default Re: Trouble with a simple if statement.


    Murdoc wrote: 
    > > 0,0,1) |
    > > +----------------------------------------------------------------------------+ 
    > > 1 |
    > > +----------------------------------------------------------------------------+
    > > 1 row in set (0.00 sec)
    > >
    > >
    > > What could possibly be going wrong here?
    > >
    > > It must be an error in my syntax, but according to what I read in the
    > > manual, it should work just fine.
    > >
    > > Any input would be appreciated.
    > >
    > > Daz[/ref]
    >
    > I could be wrong, but I don't think you can do a "DELETE FROM" or an "INSERT INTO"
    > inside a "SELECT" statement.
    >
    > --[/ref]

    That would explain the problem I am experiencing. Thank you very much.
    :)

    Daz Guest

Similar Threads

  1. Trouble with LIKE statement
    By Dionubis in forum Coldfusion Database Access
    Replies: 11
    Last Post: October 8th, 05:53 AM
  2. Trouble with INSERT statement
    By Jess930 in forum Coldfusion - Getting Started
    Replies: 4
    Last Post: June 14th, 04:55 PM
  3. Simple If statement question
    By quovadimus02 in forum Macromedia ColdFusion
    Replies: 2
    Last Post: March 22nd, 06:37 PM
  4. Simple PHP & MySQL Insert Statement Does Not Work
    By Mark Davenport in forum PHP Development
    Replies: 2
    Last Post: August 15th, 02:22 PM
  5. simple graphic in php with print statement?
    By entoone in forum PHP Development
    Replies: 1
    Last Post: August 5th, 06:55 AM

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