Jump to content

SQL Syntax Error


flash gordon

Recommended Posts

Apparently I have a syntax error. Can anyone tell me what it is?

IF TRUE
THEN
  SELECT * FROM contact_chat
ELSE
  SELECT * FROM contact_phone
END IF

 

MySQL Version: MySQL 5.0.75-0ubuntu10.2 via TCP/IP

 

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 'IF TRUE

THEN

  SELECT * FROM contact_chat

ELSE

  SELECT * FROM contact_phone' at line 1

Link to comment
Share on other sites

The syntax you are attempting is for stored procedures and functions -

This section describes the syntax for the BEGIN ... END  compound statement and other statements that can be used in the body of stored programs

 

What is your whole query and it would really help if you post the exact error message.

Link to comment
Share on other sites

I do not think it is possible to do what you want in MySQL, at least in a query. I have tried many combination's, referred to the manual etc and I do not think you can dynamically define which table to pull the data from as shown. However, if you can figure out a WHERE statement in which you can use the case's to filter the data from the tables, you may be able to achieve this.

Link to comment
Share on other sites

Because you're missing "SELECT" from the beginning. CASE is a control flow function, not a data manipulator function like SELECT is. (If I'm wrong, somebody please correct that statement)

 

You'd probably be better off doing this on the other side (PHP or whatever) or make a stored procedure/function to do this.

Link to comment
Share on other sites

On that page, after the first paragraph:

Note

There is also an IF() function, which differs from the IF statement described here. See Section 11.3, “Control Flow Functions”.

 

If you look at the example you'll see the one you were looking at was for functions/procedures:

DELIMITER //

CREATE FUNCTION SimpleCompare(n INT, m INT)
  RETURNS VARCHAR(20)

  BEGIN
    DECLARE s VARCHAR(20);

    IF n > m THEN SET s = '>';
    ELSEIF n = m THEN SET s = '=';
    ELSE SET s = '<';
    END IF;

    SET s = CONCAT(n, ' ', s, ' ', m);

    RETURN s;
  END //

DELIMITER ;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.