Connecting Tech Pros Worldwide Forums | Help | Site Map

IF statement in MySQL

Member
 
Join Date: Apr 2008
Location: Philippines
Posts: 35
#1: Oct 19 '09
Can you do something like:
Expand|Select|Wrap|Line Numbers
  1. select itemType from reservables where reservableID = #input#
  2. if (itemType='room')
  3.  select * from rooms
  4. else select * from items
Something like that? Thanks.

Kind regards
JR

Member
 
Join Date: Apr 2008
Location: Philippines
Posts: 35
#2: Oct 19 '09

re: IF statement in MySQL


okay, now I' trying something like this:

Quote:
SELECT IF(reservableType = 'item',
"Select * From items where items.itemID = 2",
"Select * From room where room.reservableID = 2")
FROM reservables WHERE reservables.reservableID = 2
but it still does not work. :(
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#3: Oct 19 '09

re: IF statement in MySQL


Hey.

SQL queries aren't really meant to include that level of logic.
For things like that, you would be better of trying a Stored Procedure

Something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. DELIMITER %%
  2. CREATE PROCEDURE `Example`(IN pInput VARCHAR(64))
  3. BEGIN
  4.     -- Declare all variables we plan on using
  5.     DECLARE doLoop BOOLEAN DEFAULT TRUE;
  6.     DECLARE cCondition VARCHAR(64);
  7.  
  8.     -- Create query cursors, so we can read the results.
  9.     DECLARE queryCursor CURSOR FOR 
  10.         SELECT `something`
  11.         FROM `parentTable`
  12.         WHERE `condition` = pInput;
  13.  
  14.     -- Execute the query
  15.     OPEN queryCursor;
  16.  
  17.     -- Fetch the first row of results
  18.     FETCH queryCursor INTO cCondition
  19.  
  20.     -- Execute a query based on the results
  21.     IF cCondition == 'something' THEN
  22.         SELECT stuff FROM firstTable WHERE condition = pInput;
  23.     ELSE
  24.         SELECT stuff FROM secondTable WHERE condition = pInput;
  25.     END IF
  26.  
  27.     -- Close the cursor
  28.     CLOSE queryCursor;
  29. END%%
  30. DELIMITER ;
And, once you have created the procedure, you call it in your code like so:
Expand|Select|Wrap|Line Numbers
  1. CALL `Example`('input value');
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: Oct 19 '09

re: IF statement in MySQL


You could also try to use joins to do this, if you don't like procedures.

Something like this might work:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM parentTable AS p
  2. LEFT JOIN firstTable AS f
  3.     ON f.itemID = p.itemID
  4. LEFT JOIN secondTable AS s
  5.     ON s.itemID = p.itemID
  6. WHERE p.itemType = 'Something'
  7. AND   p.itemID = 2;
Member
 
Join Date: Apr 2008
Location: Philippines
Posts: 35
#5: Oct 19 '09

re: IF statement in MySQL


wow, that makes sense! Although the code is more than twice as long, I can still use this. Didn't know there were procedures in MySQL. All I ever did was SELECT,INSERT,UPDATE, etc. Maybe I should start reading about MySQL more. :-/

Thanks, this was quite helpful.

Regards,
JR
Reply

Tags
conditionals, mysql