title: Error when creating Index for table Mysql
body: <p>I'm working on create index for table with mysql.
  <br> I've 2 tables:
  <br> 1. account<br>
  <a href="https://i.stack.imgur.com/qaXhz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qaXhz.jpg" alt="enter image description here"></a>
  <br> 2. x_activity (x is the account_id related to "account" table, EX: 1_activity, 2_activity).
  <a href="https://i.stack.imgur.com/q9Plh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q9Plh.jpg" alt="enter image description here"></a>
  <br>So i've created an "Index" for activity table:
  <br> Here are my code:</p>
  
  <pre><code>DROP PROCEDURE if exists update_index_for_table;
  DELIMITER $$
  CREATE PROCEDURE update_index_for_table()
  BEGIN
  
    DECLARE done INT DEFAULT FALSE;
    DECLARE accountid INT;  
    --
    -- GET ALL ACCOUNT ID
    --
  
    DECLARE accountids CURSOR FOR SELECT account_id FROM account;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  
    --
    -- LOOP 
    --  
  
    OPEN accountids; 
    read_loop: LOOP 
      FETCH accountids INTO accountid;
      IF done THEN 
        LEAVE read_loop;
      END IF;   
  
      --
      -- INDEX FOR ACTIVITY 
      --  
  
          SET @update_activity_table_1 = CONCAT("
          IF (
              SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
              WHERE `TABLE_SCHEMA` = DATABASE() AND TABLE_NAME='",accountid,"_activity' AND 
              INDEX_NAME='IDX_",accountid,"_ACTIVITY_ACTIVITY_ID'
              ) != 1
          THEN
              ALTER TABLE ",accountid,"_activity 
                  ADD KEY `IDX_",accountid,"_ACTIVITY_ACTIVITY_ID` (`activity_id`);
          END IF;
          ");
      PREPARE stmt from @update_activity_table_1; 
      EXECUTE stmt;
  
      END LOOP; 
    CLOSE accountids;
  END$$
  DELIMITER ;
  CALL update_index_for_table();
  </code></pre>
  
  <p><br> But then, for some php/mysql version (i think), its cause an error like this:</p>
  
  <p><em>#1064 - 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 (
          SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
          WHERE `TABLE_SCHEM' at line 1</em></p>
  
  <p>I've tested this code and its work fine:</p>
  
  <pre><code>SELECT COUNT(1) FROM INFORMATION_SCHEMA.STATISTICS
  WHERE `TABLE_SCHEMA` = DATABASE() AND TABLE_NAME='",accountid,"_activity' AND 
  INDEX_NAME='IDX_",accountid,"_ACTIVITY_ACTIVITY_ID'
  </code></pre>
  
  <p>Here are my php/sql version: <br>
  phpmyadmin: 4.8.5, php version: 7.2.7, mysql: 5.6.45<br><br></p>
  
  <p>Please help, thanks.</p>