title: Using IF statements in MySQL
body: <p>So i have 2 tables of this format,</p>
  
  <p><strong>actual_data:</strong></p>
  
  <pre><code>ID,ROLL_NO,ADM_DT,FEES
  1,9895987650,2017-11-14,10000
  2,78578686,2017-11-10,130
  3,434343,2017-11-15,20
  </code></pre>
  
  <p><strong>temp_data:</strong></p>
  
  <pre><code>ID,R_NO,FS,FT
  1,9895987650,9000,F1
  5,9895987650,5000,F2
  3,78578686,150,F3
  4,123456,24141,F4
  </code></pre>
  
  <p>I'm trying to create a stored-procedure here,</p>
  
  <pre><code>DELIMITER $$    
  CREATE PROCEDURE `myproc`()
  BEGIN
  SELECT a.*,b.* FROM actual_data a, temp_data b
      WHERE a.ROLL_NO = b.R_NO 
          AND 
          ((b.FS &gt;= a.FEES AND b.FT IS NOT NULL) 
          OR 
          (b.FS &lt; a.FEES AND b.FT IS NOT NULL)); 
  END $$
  DELIMITER;  
  </code></pre>
  
  <p>This will give me the following result:</p>
  
  <pre><code>ID,ROLL_NO,   ADM_DT,     FEES,ID, R_NO,FS,              FT
  1, 9895987650,2017-11-14, 10000,   1,   9895987650,9000, F1
  1, 9895987650,2017-11-14, 10000,   5,   9895987650,5000, F2
  2, 78578686,  2017-11-10, 130,     3,   78578686,150,    F3
  </code></pre>
  
  <p>Now this is where i'm getting stuck. </p>
  
  <p>What i would like to do is, </p>
  
  <p>IF <code>b.FS &gt;= a.FEES AND b.FT IS NOT NULL</code> is <code>TRUE</code>, i would like to <code>select R_NO,sum(FEES) GROUP BY R_NO HAVING sum(FEES) &gt; 1000 ORDER BY R_NO</code> from the above query result.</p>
  
  <p>ELSE IF <code>b.FS &lt; a.FEES AND b.FT IS NOT NULL</code> is <code>TRUE</code>, i would like to <code>select R_NO,sum(FEES) GROUP BY R_NO HAVING sum(FEES) &lt; 1000 ORDER BY R_NO</code> from the above query result.</p>
  
  <p>Is this possible to do through a stored-procedure? If so, could I get some suggestions as to how this can be done? I'm a newbie when it comes to MySql, so kindly help me out. </p>
  
  <p>Following is my effort:</p>
  
  <pre><code>DELIMITER $$ 
  DROP PROCEDURE IF EXISTS `myproc` $$    
  CREATE PROCEDURE `myproc`()
  BEGIN
  DECLARE firstCond INT;
  DECLARE secondCond INT;
  CREATE TEMPORARY TABLE firstTemp
  SELECT a.*,b.R_NO,b.FS,b.FT FROM actual_data a, temp_data b
      WHERE a.ROLL_NO = b.R_NO AND (b.FS &gt;= a.FEES AND b.FT IS NOT NULL);
  
  CREATE TEMPORARY TABLE secondTemp
  SELECT a.*,b.R_NO,b.FS,b.FT FROM actual_data a, temp_data b
      WHERE a.ROLL_NO = b.R_NO AND (b.FS &lt; a.FEES AND b.FT IS NOT NULL);
  
  SELECT COUNT(1) INTO firstCond FROM firstTemp;
  SELECT COUNT(1) INTO secondCond FROM secondTemp;
  
  IF firstCond &gt; 0 THEN
      SELECT f.ID,f.R_NO,SUM(f.FS) AS FS INTO OUTFILE 'C:/Users/MySqlProcFirstOutput1.txt'
      FIELDS TERMINATED BY ','  
      LINES TERMINATED BY '\n'  
      FROM firstTemp f 
          GROUP BY f.R_NO,f.ID 
              HAVING SUM(f.FS) &gt; 50 
                  ORDER BY f.R_NO,f.ID ASC;
  END IF;
  IF secondCond &gt; 0 THEN
      SELECT s.ID,s.R_NO,SUM(s.FS) AS FS INTO OUTFILE 'C:/Users/Output2.txt' 
      FIELDS TERMINATED BY ','  
      LINES TERMINATED BY '\n' 
      FROM secondTemp s 
          GROUP BY s.R_NO,s.ID 
              HAVING SUM(s.FS) &gt; 50 
                  ORDER BY s.R_NO,s.ID ASC;
  END IF;
  END $$
  DELIMITER ;
  </code></pre>
  
  <p>What I'd like to know is, whether this is the optimized way to do this? </p>