DELIMITER $$
CREATE PROCEDURE `wp_dragonecommercezyx987_getChargetypeList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN chargetypestatus int,
   OUT chargetypetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @chargetypestatus = 0;
   set @chargetypestatus2 = 1;
   if chargetypestatus = -1 then set @chargetypestatus = 0; set @chargetypestatus2 = 1; end if;
   if chargetypestatus = 0 then set @chargetypestatus = 0; set @chargetypestatus2 = 0; end if;
   if chargetypestatus = 1 then set @chargetypestatus = 1; set @chargetypestatus2 = 1; end if;
      
   if searchwords = '' then SET @statement = concat( 'Select count(*) into @chargetypetotalquantity from ', 'wp_dragonecommercezyx987_chargetype WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @chargetypestatus, @chargetypestatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @chargetypetotalquantity from ', 'wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET chargetypetotalquantity = @chargetypetotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = "SELECT * from wp_dragonecommercezyx987_chargetype WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypestatus, @chargetypestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$  $$
--   ----------------------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getChart1Data`(
   IN frequency varchar(200)
)
BEGIN
   set @fromdate1 = '1000-01-01 00:00:00';
   set @todate1 = '9999-12-31 23:59:59';
   set @fromdate2 = '1000-01-01 00:00:00';
   set @todate2 = '9999-12-31 23:59:59';
   set @fromdate3 = '1000-01-01 00:00:00';
   set @todate3 = '9999-12-31 23:59:59';
   set @fromdate4 = '1000-01-01 00:00:00';
   set @todate4 = '9999-12-31 23:59:59';
   set @fromdate5 = '1000-01-01 00:00:00';
   set @todate5 = '9999-12-31 23:59:59';
   set @fromdate6 = '1000-01-01 00:00:00';
   set @todate6 = '9999-12-31 23:59:59';
   set @fromdate7 = '1000-01-01 00:00:00';
   set @todate7 = '9999-12-31 23:59:59';
   set @fromdate8 = '1000-01-01 00:00:00';
   set @todate8 = '9999-12-31 23:59:59';
   
   set @fromdate9 = '1000-01-01 00:00:00';
   set @todate9 = '9999-12-31 23:59:59';
   set @fromdate10 = '1000-01-01 00:00:00';
   set @todate10 = '9999-12-31 23:59:59';
   set @fromdate11 = '1000-01-01 00:00:00';
   set @todate11 = '9999-12-31 23:59:59';
   set @fromdate12 = '1000-01-01 00:00:00';
   set @todate12 = '9999-12-31 23:59:59';
   set @fromdate13 = '1000-01-01 00:00:00';
   set @todate13 = '9999-12-31 23:59:59';
   set @fromdate14 = '1000-01-01 00:00:00';
   set @todate14 = '9999-12-31 23:59:59';
   set @fromdate15 = '1000-01-01 00:00:00';
   set @todate15 = '9999-12-31 23:59:59';
   set @fromdate16 = '1000-01-01 00:00:00';
   set @todate16 = '9999-12-31 23:59:59'; 
   
   set @ordertotalquantity1 = 0;
   set @ordertotalquantity2 = 0;
   set @ordertotalquantity3 = 0;
   set @ordertotalquantity4 = 0;
   set @ordertotalquantity5 = 0;
   set @ordertotalquantity6 = 0;
   set @ordertotalquantity7 = 0;
   set @ordertotalquantity8 = 0;                     
   set @ordertotalquantity9 = 0;
   set @ordertotalquantity10 = 0;
   set @ordertotalquantity11 = 0;
   set @ordertotalquantity12 = 0;
   set @ordertotalquantity13 = 0;
   set @ordertotalquantity14 = 0;
   set @ordertotalquantity15 = 0;
   set @ordertotalquantity16 = 0;                    
   
   set @frequency = frequency;
   if frequency = '14days' then
   	   set @todate14 = CURDATE() + INTERVAL 1 DAY;
 	   set @fromdate14 = DATE_SUB(@todate14, INTERVAL 1 DAY);
	   set @todate13 = DATE_SUB(@todate14, INTERVAL 1 DAY);       
 	   set @fromdate13 = DATE_SUB(@todate14, INTERVAL 2 DAY);
 	   set @todate12 = DATE_SUB(@todate14, INTERVAL 2 DAY);      
  	   set @fromdate12 = DATE_SUB(@todate14, INTERVAL 3 DAY);
	   set @todate11 = DATE_SUB(@todate14, INTERVAL 3 DAY);
	   set @fromdate11 = DATE_SUB(@todate14, INTERVAL 4 DAY);
  	   set @todate10 = DATE_SUB(@todate14, INTERVAL 4 DAY);
	   set @fromdate10 = DATE_SUB(@todate14, INTERVAL 5 DAY);
 	   set @todate9 = DATE_SUB(@todate14, INTERVAL 5 DAY);
 	   set @fromdate9 = DATE_SUB(@todate14, INTERVAL 6 DAY);
	   set @todate8 = DATE_SUB(@todate14, INTERVAL 6 DAY);	       
 	   set @fromdate8 = DATE_SUB(@todate14, INTERVAL 7 DAY);
  	   set @todate7 = DATE_SUB(@todate14, INTERVAL 7 DAY);
 	   set @fromdate7 = DATE_SUB(@todate14, INTERVAL 8 DAY);
 	   set @todate6 = DATE_SUB(@todate14, INTERVAL 8 DAY);      
	   set @fromdate6 = DATE_SUB(@todate14, INTERVAL 9 DAY);
 	   set @todate5 = DATE_SUB(@todate14, INTERVAL 9 DAY);      
 	   set @fromdate5 = DATE_SUB(@todate14, INTERVAL 10 DAY);      
 	   set @todate4 = DATE_SUB(@todate14, INTERVAL 10 DAY);
 	   set @fromdate4 = DATE_SUB(@todate14, INTERVAL 11 DAY);      
 	   set @todate3 = DATE_SUB(@todate14, INTERVAL 11 DAY);      
	   set @fromdate3 = DATE_SUB(@todate14, INTERVAL 12 DAY);
  	   set @todate2 = DATE_SUB(@todate14, INTERVAL 12 DAY);     
 	   set @fromdate2 = DATE_SUB(@todate14, INTERVAL 13 DAY);
 	   set @todate1 = DATE_SUB(@todate14, INTERVAL 13 DAY);      
	   set @fromdate1 = DATE_SUB(@todate14, INTERVAL 14 DAY);
	   set @fromdate15 = '1000-01-01 00:00:00';
	   set @todate15 = '1000-01-01 00:00:00';
	   set @fromdate16 = '1000-01-01 00:00:00';
	   set @todate16 = '1000-01-01 00:00:00';
   end if;   
   if frequency = '16weeks' then 
	   set @todate16 = (curdate()+interval 7 - weekday(curdate()) day);            
	   set @fromdate16 = DATE_SUB(@todate16, INTERVAL 1 WEEK);
       set @todate15 = DATE_SUB(@todate16, INTERVAL 1 WEEK);
	   set @fromdate15 = DATE_SUB(@todate16, INTERVAL 2 WEEK);           
 	   set @todate14 = DATE_SUB(@todate16, INTERVAL 2 WEEK);          
  	   set @fromdate14 = DATE_SUB(@todate16, INTERVAL 3 WEEK);        
 	   set @todate13 = DATE_SUB(@todate16, INTERVAL 3 WEEK);          
  	   set @fromdate13 = DATE_SUB(@todate16, INTERVAL 4 WEEK);        
 	   set @todate12 = DATE_SUB(@todate16, INTERVAL 4 WEEK);          
  	   set @fromdate12 = DATE_SUB(@todate16, INTERVAL 5 WEEK);        
 	   set @todate11 = DATE_SUB(@todate16, INTERVAL 5 WEEK);          
  	   set @fromdate11 = DATE_SUB(@todate16, INTERVAL 6 WEEK);        
 	   set @todate10 = DATE_SUB(@todate16, INTERVAL 6 WEEK);          
  	   set @fromdate10 = DATE_SUB(@todate16, INTERVAL 7 WEEK);
 	   set @todate9 = DATE_SUB(@todate16, INTERVAL 7 WEEK);          
  	   set @fromdate9 = DATE_SUB(@todate16, INTERVAL 8 WEEK);       
  	   set @todate8 = DATE_SUB(@todate16, INTERVAL 8 WEEK);          
  	   set @fromdate8 = DATE_SUB(@todate16, INTERVAL 9 WEEK);       
  	   set @todate7 = DATE_SUB(@todate16, INTERVAL 9 WEEK);          
  	   set @fromdate7 = DATE_SUB(@todate16, INTERVAL 10 WEEK);       
  	   set @todate6 = DATE_SUB(@todate16, INTERVAL 10 WEEK);          
  	   set @fromdate6 = DATE_SUB(@todate16, INTERVAL 11 WEEK);       
  	   set @todate5 = DATE_SUB(@todate16, INTERVAL 11 WEEK);          
  	   set @fromdate5 = DATE_SUB(@todate16, INTERVAL 12 WEEK);
  	   set @todate4 = DATE_SUB(@todate16, INTERVAL 12 WEEK);          
  	   set @fromdate4 = DATE_SUB(@todate16, INTERVAL 13 WEEK);       
  	   set @todate3 = DATE_SUB(@todate16, INTERVAL 13 WEEK);          
  	   set @fromdate3 = DATE_SUB(@todate16, INTERVAL 14 WEEK);        
  	   set @todate2 = DATE_SUB(@todate16, INTERVAL 14 WEEK);          
  	   set @fromdate2 = DATE_SUB(@todate16, INTERVAL 15 WEEK);        
   	   set @todate1 = DATE_SUB(@todate16, INTERVAL 15 WEEK);          
  	   set @fromdate1 = DATE_SUB(@todate16, INTERVAL 16 WEEK);      
   end if;
  if frequency = '12months' then 
	   set @todate12 = adddate(last_day(curdate()), 1);
       set @fromdate12 = DATE_FORMAT(NOW() ,'%Y-%m-01');       
	   set @todate11 = DATE_FORMAT(NOW() ,'%Y-%m-01'); 
	   set @fromdate11 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 1 month);          
 	   set @todate10 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 1 month);          
  	   set @fromdate10 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 2 month);       
 	   set @todate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 2 month);          
  	   set @fromdate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 3 month);       
 	   set @todate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 3 month);           
  	   set @fromdate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 4 month);         
 	   set @todate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 4 month);           
  	   set @fromdate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 5 month);       
 	   set @todate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 5 month);          
  	   set @fromdate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 6 month);
 	   set @todate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 6 month);          
  	   set @fromdate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 7 month);       
  	   set @todate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 7 month);           
  	   set @fromdate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 8 month);       
  	   set @todate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 8 month);           
  	   set @fromdate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 9 month);        
  	   set @todate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 9 month);          
  	   set @fromdate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 10 month);       
  	   set @todate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 10 month);          
  	   set @fromdate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 11 month);
  	   set @todate13 = '1000-01-01 00:00:00';          
  	   set @fromdate13 = '1000-01-01 00:00:00';      
  	   set @todate14 = '1000-01-01 00:00:00';          
  	   set @fromdate14 = '1000-01-01 00:00:00';        
  	   set @todate15 = '1000-01-01 00:00:00';          
  	   set @fromdate15 = '1000-01-01 00:00:00';       
   	   set @todate16 = '1000-01-01 00:00:00';          
  	   set @fromdate16 = '1000-01-01 00:00:00';              
   end if;
   if frequency = '10years' then 
 	   set @todate10 = CONCAT(YEAR(curdate())+1, '-01-01');
       set @fromdate10 = DATE_FORMAT(NOW() ,'%Y-01-01');       
	   set @todate9 = DATE_FORMAT(NOW() ,'%Y-01-01');
	   set @fromdate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 1 year);          
 	   set @todate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 1 year);          
  	   set @fromdate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 2 year);       
 	   set @todate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 2 year);          
  	   set @fromdate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 3 year);       
 	   set @todate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 3 year);           
  	   set @fromdate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 4 year);         
 	   set @todate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 4 year);           
  	   set @fromdate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 5 year);       
 	   set @todate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 5 year);          
  	   set @fromdate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 6 year);
 	   set @todate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 6 year);          
  	   set @fromdate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 7 year);       
  	   set @todate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 7 year);           
  	   set @fromdate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 8 year);       
  	   set @todate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 8 year);           
  	   set @fromdate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 9 year);
  	   set @todate16 = '1000-01-01 00:00:00';          
  	   set @fromdate16 = '1000-01-01 00:00:00';       
  	   set @todate15 = '1000-01-01 00:00:00';          
  	   set @fromdate15 = '1000-01-01 00:00:00'; 
  	   set @todate14 = '1000-01-01 00:00:00';          
  	   set @fromdate14 = '1000-01-01 00:00:00';      
  	   set @todate13 = '1000-01-01 00:00:00';          
  	   set @fromdate13 = '1000-01-01 00:00:00';        
  	   set @todate12 = '1000-01-01 00:00:00';          
  	   set @fromdate12 = '1000-01-01 00:00:00';       
   	   set @todate11 = '1000-01-01 00:00:00';          
  	   set @fromdate11 = '1000-01-01 00:00:00';
   end if;
   
   set @timezonehours = '+00:00';
   SET @statement = concat( 'Select value into @timezonehours from ', "(SELECT value FROM wp_dragonecommercezyx987_setting WHERE name = 'COMPANY_TIMEZONE_HOURS') a" ); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt; 
   if @timezonehours = '' or @timezonehours is null then 
       set @timezonehours = '+00:00';
   end if;

    if frequency = '14days' || frequency = '16weeks' || frequency = '12months' || frequency = '10years' then 
       SET @statement = concat( 'Select total into @ordertotalquantity1 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate1, @todate1;
       DEALLOCATE PREPARE stmt; 
       SET @statement = concat( 'Select total into @ordertotalquantity2 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate2, @todate2;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity3 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate3, @todate3;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity4 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate4, @todate4;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity5 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate5, @todate5;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity6 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate6, @todate6;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity7 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate7, @todate7;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity8 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" );  
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate8, @todate8;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity9 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate9, @todate9;
       DEALLOCATE PREPARE stmt; 
       SET @statement = concat( 'Select total into @ordertotalquantity10 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate10, @todate10;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity11 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate11, @todate11;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity12 from ',"(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate12, @todate12;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity13 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate13, @todate13;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity14 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate14, @todate14;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity15 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate15, @todate15;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity16 from ', "(SELECT sum(totalfee) 'total' FROM wp_dragonecommercezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate16, @todate16;
       DEALLOCATE PREPARE stmt;       
    end if;        
                
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if frequency = '14days'   then SET @sqlv = concat("Select @ordertotalquantity1 '" , @fromdate1, "', @ordertotalquantity2 '" , @fromdate2, "', @ordertotalquantity3 '" , @fromdate3, "', @ordertotalquantity4 '" , @fromdate4, "', @ordertotalquantity5 '" , @fromdate5, "', @ordertotalquantity6 '" , @fromdate6, "', @ordertotalquantity7 '" , @fromdate7, "', @ordertotalquantity8 '" , @fromdate8, "', @ordertotalquantity9 '" , @fromdate9, "', @ordertotalquantity10 '" , @fromdate10, "', @ordertotalquantity11 '" , @fromdate11, "', @ordertotalquantity12 '" , @fromdate12, "', @ordertotalquantity13 '" , @fromdate13, "', @ordertotalquantity14  '" , @fromdate14, "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '16weeks'   then SET @sqlv = concat("Select @ordertotalquantity1 '" , @fromdate1, "', @ordertotalquantity2 '" , @fromdate2, "', @ordertotalquantity3 '" , @fromdate3, "', @ordertotalquantity4 '" , @fromdate4, "', @ordertotalquantity5 '" , @fromdate5, "', @ordertotalquantity6 '" , @fromdate6, "', @ordertotalquantity7 '" , @fromdate7, "', @ordertotalquantity8 '" , @fromdate8, "', @ordertotalquantity9 '" , @fromdate9, "', @ordertotalquantity10 '" , @fromdate10, "', @ordertotalquantity11 '" , @fromdate11, "', @ordertotalquantity12 '" , @fromdate12, "', @ordertotalquantity13 '" , @fromdate13, "', @ordertotalquantity14  '" , @fromdate14, "', @ordertotalquantity15 '" , @fromdate15, "', @ordertotalquantity16  '" , @fromdate16, "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '12months'  then SET @sqlv = concat("Select @ordertotalquantity1 '" , DATE_FORMAT(@fromdate1 ,'%Y-%m'), "', @ordertotalquantity2 '" , DATE_FORMAT(@fromdate2 ,'%Y-%m'), "', @ordertotalquantity3 '" , DATE_FORMAT(@fromdate3 ,'%Y-%m'), "', @ordertotalquantity4 '" , DATE_FORMAT(@fromdate4 ,'%Y-%m'), "', @ordertotalquantity5 '" , DATE_FORMAT(@fromdate5 ,'%Y-%m'), "', @ordertotalquantity6 '" , DATE_FORMAT(@fromdate6 ,'%Y-%m'), "', @ordertotalquantity7 '" , DATE_FORMAT(@fromdate7 ,'%Y-%m'), "', @ordertotalquantity8 '" , DATE_FORMAT(@fromdate8 ,'%Y-%m'), "', @ordertotalquantity9 '" , DATE_FORMAT(@fromdate9 ,'%Y-%m'), "', @ordertotalquantity10 '" , DATE_FORMAT(@fromdate10 ,'%Y-%m'), "', @ordertotalquantity11 '" , DATE_FORMAT(@fromdate11 ,'%Y-%m'), "', @ordertotalquantity12 '" , DATE_FORMAT(@fromdate12 ,'%Y-%m'), "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '10years'  then SET @sqlv = concat("Select @ordertotalquantity1 '" , DATE_FORMAT(@fromdate1 ,'%Y'), "', @ordertotalquantity2 '" , DATE_FORMAT(@fromdate2 ,'%Y'), "', @ordertotalquantity3 '" , DATE_FORMAT(@fromdate3 ,'%Y'), "', @ordertotalquantity4 '" , DATE_FORMAT(@fromdate4 ,'%Y'), "', @ordertotalquantity5 '" , DATE_FORMAT(@fromdate5 ,'%Y'), "', @ordertotalquantity6 '" , DATE_FORMAT(@fromdate6 ,'%Y'), "', @ordertotalquantity7 '" , DATE_FORMAT(@fromdate7 ,'%Y'), "', @ordertotalquantity8 '" , DATE_FORMAT(@fromdate8 ,'%Y'), "', @ordertotalquantity9 '" , DATE_FORMAT(@fromdate9 ,'%Y'), "', @ordertotalquantity10 '" , DATE_FORMAT(@fromdate10 ,'%Y'), "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ; 
       DEALLOCATE PREPARE stmt2;
    end if;
    
END  $$  $$

--  ----------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getChart2Data`(
   IN frequency varchar(200)
)
BEGIN
   set @fromdate1 = '1000-01-01 00:00:00';
   set @todate1 = '9999-12-31 23:59:59';
   set @fromdate2 = '1000-01-01 00:00:00';
   set @todate2 = '9999-12-31 23:59:59';
   set @fromdate3 = '1000-01-01 00:00:00';
   set @todate3 = '9999-12-31 23:59:59';
   set @fromdate4 = '1000-01-01 00:00:00';
   set @todate4 = '9999-12-31 23:59:59';
   set @fromdate5 = '1000-01-01 00:00:00';
   set @todate5 = '9999-12-31 23:59:59';
   set @fromdate6 = '1000-01-01 00:00:00';
   set @todate6 = '9999-12-31 23:59:59';
   set @fromdate7 = '1000-01-01 00:00:00';
   set @todate7 = '9999-12-31 23:59:59';
   set @fromdate8 = '1000-01-01 00:00:00';
   set @todate8 = '9999-12-31 23:59:59';
   
   set @fromdate9 = '1000-01-01 00:00:00';
   set @todate9 = '9999-12-31 23:59:59';
   set @fromdate10 = '1000-01-01 00:00:00';
   set @todate10 = '9999-12-31 23:59:59';
   set @fromdate11 = '1000-01-01 00:00:00';
   set @todate11 = '9999-12-31 23:59:59';
   set @fromdate12 = '1000-01-01 00:00:00';
   set @todate12 = '9999-12-31 23:59:59';
   set @fromdate13 = '1000-01-01 00:00:00';
   set @todate13 = '9999-12-31 23:59:59';
   set @fromdate14 = '1000-01-01 00:00:00';
   set @todate14 = '9999-12-31 23:59:59';
   set @fromdate15 = '1000-01-01 00:00:00';
   set @todate15 = '9999-12-31 23:59:59';
   set @fromdate16 = '1000-01-01 00:00:00';
   set @todate16 = '9999-12-31 23:59:59'; 
   
   set @ordertotalquantity1 = 0;
   set @ordertotalquantity2 = 0;
   set @ordertotalquantity3 = 0;
   set @ordertotalquantity4 = 0;
   set @ordertotalquantity5 = 0;
   set @ordertotalquantity6 = 0;
   set @ordertotalquantity7 = 0;
   set @ordertotalquantity8 = 0;                     
   set @ordertotalquantity9 = 0;
   set @ordertotalquantity10 = 0;
   set @ordertotalquantity11 = 0;
   set @ordertotalquantity12 = 0;
   set @ordertotalquantity13 = 0;
   set @ordertotalquantity14 = 0;
   set @ordertotalquantity15 = 0;
   set @ordertotalquantity16 = 0;                    
   
   set @frequency = frequency;
   if frequency = '14days' then
   	   set @todate14 = CURDATE() + INTERVAL 1 DAY;
 	   set @fromdate14 = DATE_SUB(@todate14, INTERVAL 1 DAY);
	   set @todate13 = DATE_SUB(@todate14, INTERVAL 1 DAY);       
 	   set @fromdate13 = DATE_SUB(@todate14, INTERVAL 2 DAY);
 	   set @todate12 = DATE_SUB(@todate14, INTERVAL 2 DAY);      
  	   set @fromdate12 = DATE_SUB(@todate14, INTERVAL 3 DAY);
	   set @todate11 = DATE_SUB(@todate14, INTERVAL 3 DAY);
	   set @fromdate11 = DATE_SUB(@todate14, INTERVAL 4 DAY);
  	   set @todate10 = DATE_SUB(@todate14, INTERVAL 4 DAY);
	   set @fromdate10 = DATE_SUB(@todate14, INTERVAL 5 DAY);
 	   set @todate9 = DATE_SUB(@todate14, INTERVAL 5 DAY);
 	   set @fromdate9 = DATE_SUB(@todate14, INTERVAL 6 DAY);
	   set @todate8 = DATE_SUB(@todate14, INTERVAL 6 DAY);	       
 	   set @fromdate8 = DATE_SUB(@todate14, INTERVAL 7 DAY);
  	   set @todate7 = DATE_SUB(@todate14, INTERVAL 7 DAY);
 	   set @fromdate7 = DATE_SUB(@todate14, INTERVAL 8 DAY);
 	   set @todate6 = DATE_SUB(@todate14, INTERVAL 8 DAY);      
	   set @fromdate6 = DATE_SUB(@todate14, INTERVAL 9 DAY);
 	   set @todate5 = DATE_SUB(@todate14, INTERVAL 9 DAY);      
 	   set @fromdate5 = DATE_SUB(@todate14, INTERVAL 10 DAY);      
 	   set @todate4 = DATE_SUB(@todate14, INTERVAL 10 DAY);
 	   set @fromdate4 = DATE_SUB(@todate14, INTERVAL 11 DAY);      
 	   set @todate3 = DATE_SUB(@todate14, INTERVAL 11 DAY);      
	   set @fromdate3 = DATE_SUB(@todate14, INTERVAL 12 DAY);
  	   set @todate2 = DATE_SUB(@todate14, INTERVAL 12 DAY);     
 	   set @fromdate2 = DATE_SUB(@todate14, INTERVAL 13 DAY);
 	   set @todate1 = DATE_SUB(@todate14, INTERVAL 13 DAY);      
	   set @fromdate1 = DATE_SUB(@todate14, INTERVAL 14 DAY);
	   set @fromdate15 = '1000-01-01 00:00:00';
	   set @todate15 = '1000-01-01 00:00:00';
	   set @fromdate16 = '1000-01-01 00:00:00';
	   set @todate16 = '1000-01-01 00:00:00';
   end if;   
   if frequency = '16weeks' then 
	   set @todate16 = (curdate()+interval 7 - weekday(curdate()) day);            
	   set @fromdate16 = DATE_SUB(@todate16, INTERVAL 1 WEEK);
       set @todate15 = DATE_SUB(@todate16, INTERVAL 1 WEEK);
	   set @fromdate15 = DATE_SUB(@todate16, INTERVAL 2 WEEK);           
 	   set @todate14 = DATE_SUB(@todate16, INTERVAL 2 WEEK);          
  	   set @fromdate14 = DATE_SUB(@todate16, INTERVAL 3 WEEK);        
 	   set @todate13 = DATE_SUB(@todate16, INTERVAL 3 WEEK);          
  	   set @fromdate13 = DATE_SUB(@todate16, INTERVAL 4 WEEK);        
 	   set @todate12 = DATE_SUB(@todate16, INTERVAL 4 WEEK);          
  	   set @fromdate12 = DATE_SUB(@todate16, INTERVAL 5 WEEK);        
 	   set @todate11 = DATE_SUB(@todate16, INTERVAL 5 WEEK);          
  	   set @fromdate11 = DATE_SUB(@todate16, INTERVAL 6 WEEK);        
 	   set @todate10 = DATE_SUB(@todate16, INTERVAL 6 WEEK);          
  	   set @fromdate10 = DATE_SUB(@todate16, INTERVAL 7 WEEK);
 	   set @todate9 = DATE_SUB(@todate16, INTERVAL 7 WEEK);          
  	   set @fromdate9 = DATE_SUB(@todate16, INTERVAL 8 WEEK);       
  	   set @todate8 = DATE_SUB(@todate16, INTERVAL 8 WEEK);          
  	   set @fromdate8 = DATE_SUB(@todate16, INTERVAL 9 WEEK);       
  	   set @todate7 = DATE_SUB(@todate16, INTERVAL 9 WEEK);          
  	   set @fromdate7 = DATE_SUB(@todate16, INTERVAL 10 WEEK);       
  	   set @todate6 = DATE_SUB(@todate16, INTERVAL 10 WEEK);          
  	   set @fromdate6 = DATE_SUB(@todate16, INTERVAL 11 WEEK);       
  	   set @todate5 = DATE_SUB(@todate16, INTERVAL 11 WEEK);          
  	   set @fromdate5 = DATE_SUB(@todate16, INTERVAL 12 WEEK);
  	   set @todate4 = DATE_SUB(@todate16, INTERVAL 12 WEEK);          
  	   set @fromdate4 = DATE_SUB(@todate16, INTERVAL 13 WEEK);       
  	   set @todate3 = DATE_SUB(@todate16, INTERVAL 13 WEEK);          
  	   set @fromdate3 = DATE_SUB(@todate16, INTERVAL 14 WEEK);        
  	   set @todate2 = DATE_SUB(@todate16, INTERVAL 14 WEEK);          
  	   set @fromdate2 = DATE_SUB(@todate16, INTERVAL 15 WEEK);        
   	   set @todate1 = DATE_SUB(@todate16, INTERVAL 15 WEEK);          
  	   set @fromdate1 = DATE_SUB(@todate16, INTERVAL 16 WEEK);      
   end if;
  if frequency = '12months' then 
	   set @todate12 = adddate(last_day(curdate()), 1);
       set @fromdate12 = DATE_FORMAT(NOW() ,'%Y-%m-01');       
	   set @todate11 = DATE_FORMAT(NOW() ,'%Y-%m-01'); 
	   set @fromdate11 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 1 month);          
 	   set @todate10 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 1 month);          
  	   set @fromdate10 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 2 month);       
 	   set @todate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 2 month);          
  	   set @fromdate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 3 month);       
 	   set @todate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 3 month);           
  	   set @fromdate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 4 month);         
 	   set @todate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 4 month);           
  	   set @fromdate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 5 month);       
 	   set @todate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 5 month);          
  	   set @fromdate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 6 month);
 	   set @todate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 6 month);          
  	   set @fromdate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 7 month);       
  	   set @todate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 7 month);           
  	   set @fromdate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 8 month);       
  	   set @todate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 8 month);           
  	   set @fromdate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 9 month);        
  	   set @todate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 9 month);          
  	   set @fromdate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 10 month);       
  	   set @todate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 10 month);          
  	   set @fromdate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 11 month);
  	   set @todate13 = '1000-01-01 00:00:00';          
  	   set @fromdate13 = '1000-01-01 00:00:00';      
  	   set @todate14 = '1000-01-01 00:00:00';          
  	   set @fromdate14 = '1000-01-01 00:00:00';        
  	   set @todate15 = '1000-01-01 00:00:00';          
  	   set @fromdate15 = '1000-01-01 00:00:00';       
   	   set @todate16 = '1000-01-01 00:00:00';          
  	   set @fromdate16 = '1000-01-01 00:00:00';              
   end if;
   if frequency = '10years' then 
 	   set @todate10 = CONCAT(YEAR(curdate())+1, '-01-01');
       set @fromdate10 = DATE_FORMAT(NOW() ,'%Y-01-01');       
	   set @todate9 = DATE_FORMAT(NOW() ,'%Y-01-01');
	   set @fromdate9 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 1 year);          
 	   set @todate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 1 year);          
  	   set @fromdate8 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 2 year);       
 	   set @todate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 2 year);          
  	   set @fromdate7 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 3 year);       
 	   set @todate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 3 year);           
  	   set @fromdate6 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 4 year);         
 	   set @todate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 4 year);           
  	   set @fromdate5 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 5 year);       
 	   set @todate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 5 year);          
  	   set @fromdate4 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 6 year);
 	   set @todate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 6 year);          
  	   set @fromdate3 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 7 year);       
  	   set @todate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 7 year);           
  	   set @fromdate2 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 8 year);       
  	   set @todate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 8 year);           
  	   set @fromdate1 = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 9 year);
  	   set @todate16 = '1000-01-01 00:00:00';          
  	   set @fromdate16 = '1000-01-01 00:00:00';       
  	   set @todate15 = '1000-01-01 00:00:00';          
  	   set @fromdate15 = '1000-01-01 00:00:00'; 
  	   set @todate14 = '1000-01-01 00:00:00';          
  	   set @fromdate14 = '1000-01-01 00:00:00';      
  	   set @todate13 = '1000-01-01 00:00:00';          
  	   set @fromdate13 = '1000-01-01 00:00:00';        
  	   set @todate12 = '1000-01-01 00:00:00';          
  	   set @fromdate12 = '1000-01-01 00:00:00';       
   	   set @todate11 = '1000-01-01 00:00:00';          
  	   set @fromdate11 = '1000-01-01 00:00:00';
   end if;
 
   set @timezonehours = '+00:00';
   SET @statement = concat( 'Select value into @timezonehours from ', "(SELECT value FROM wp_dragonecommercezyx987_setting WHERE name = 'COMPANY_TIMEZONE_HOURS') a" ); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt; 
   if @timezonehours = '' or @timezonehours is null then 
       set @timezonehours = '+00:00';
   end if; 

   if frequency = '14days' || frequency = '16weeks' || frequency = '12months' || frequency = '10years' then 
       SET @statement = concat( 'Select total into @ordertotalquantity1 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate1, @todate1;
       DEALLOCATE PREPARE stmt; 
       SET @statement = concat( 'Select total into @ordertotalquantity2 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate2, @todate2;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity3 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate3, @todate3;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity4 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate4, @todate4;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity5 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate5, @todate5;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity6 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate6, @todate6;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity7 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate7, @todate7;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity8 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" );  
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate8, @todate8;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity9 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate9, @todate9;
       DEALLOCATE PREPARE stmt; 
       SET @statement = concat( 'Select total into @ordertotalquantity10 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate10, @todate10;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity11 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate11, @todate11;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity12 from ',"(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate12, @todate12;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity13 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate13, @todate13;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity14 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate14, @todate14;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity15 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate15, @todate15;
       DEALLOCATE PREPARE stmt;
       SET @statement = concat( 'Select total into @ordertotalquantity16 from ', "(SELECT sum(quantity) 'total' FROM wp_dragonecommercezyx987_product_order po JOIN wp_dragonecommercezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND  CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate16, @todate16;
       DEALLOCATE PREPARE stmt;       
    end if;        		
    
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if frequency = '14days'   then SET @sqlv = concat("Select @ordertotalquantity1 '" , @fromdate1, "', @ordertotalquantity2 '" , @fromdate2, "', @ordertotalquantity3 '" , @fromdate3, "', @ordertotalquantity4 '" , @fromdate4, "', @ordertotalquantity5 '" , @fromdate5, "', @ordertotalquantity6 '" , @fromdate6, "', @ordertotalquantity7 '" , @fromdate7, "', @ordertotalquantity8 '" , @fromdate8, "', @ordertotalquantity9 '" , @fromdate9, "', @ordertotalquantity10 '" , @fromdate10, "', @ordertotalquantity11 '" , @fromdate11, "', @ordertotalquantity12 '" , @fromdate12, "', @ordertotalquantity13 '" , @fromdate13, "', @ordertotalquantity14  '" , @fromdate14, "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '16weeks'   then SET @sqlv = concat("Select @ordertotalquantity1 '" , @fromdate1, "', @ordertotalquantity2 '" , @fromdate2, "', @ordertotalquantity3 '" , @fromdate3, "', @ordertotalquantity4 '" , @fromdate4, "', @ordertotalquantity5 '" , @fromdate5, "', @ordertotalquantity6 '" , @fromdate6, "', @ordertotalquantity7 '" , @fromdate7, "', @ordertotalquantity8 '" , @fromdate8, "', @ordertotalquantity9 '" , @fromdate9, "', @ordertotalquantity10 '" , @fromdate10, "', @ordertotalquantity11 '" , @fromdate11, "', @ordertotalquantity12 '" , @fromdate12, "', @ordertotalquantity13 '" , @fromdate13, "', @ordertotalquantity14  '" , @fromdate14, "', @ordertotalquantity15 '" , @fromdate15, "', @ordertotalquantity16  '" , @fromdate16, "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '12months'  then SET @sqlv = concat("Select @ordertotalquantity1 '" , DATE_FORMAT(@fromdate1 ,'%Y-%m'), "', @ordertotalquantity2 '" , DATE_FORMAT(@fromdate2 ,'%Y-%m'), "', @ordertotalquantity3 '" , DATE_FORMAT(@fromdate3 ,'%Y-%m'), "', @ordertotalquantity4 '" , DATE_FORMAT(@fromdate4 ,'%Y-%m'), "', @ordertotalquantity5 '" , DATE_FORMAT(@fromdate5 ,'%Y-%m'), "', @ordertotalquantity6 '" , DATE_FORMAT(@fromdate6 ,'%Y-%m'), "', @ordertotalquantity7 '" , DATE_FORMAT(@fromdate7 ,'%Y-%m'), "', @ordertotalquantity8 '" , DATE_FORMAT(@fromdate8 ,'%Y-%m'), "', @ordertotalquantity9 '" , DATE_FORMAT(@fromdate9 ,'%Y-%m'), "', @ordertotalquantity10 '" , DATE_FORMAT(@fromdate10 ,'%Y-%m'), "', @ordertotalquantity11 '" , DATE_FORMAT(@fromdate11 ,'%Y-%m'), "', @ordertotalquantity12 '" , DATE_FORMAT(@fromdate12 ,'%Y-%m'), "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ;
       DEALLOCATE PREPARE stmt2;
    end if;
    if frequency = '10years'  then SET @sqlv = concat("Select @ordertotalquantity1 '" , DATE_FORMAT(@fromdate1 ,'%Y'), "', @ordertotalquantity2 '" , DATE_FORMAT(@fromdate2 ,'%Y'), "', @ordertotalquantity3 '" , DATE_FORMAT(@fromdate3 ,'%Y'), "', @ordertotalquantity4 '" , DATE_FORMAT(@fromdate4 ,'%Y'), "', @ordertotalquantity5 '" , DATE_FORMAT(@fromdate5 ,'%Y'), "', @ordertotalquantity6 '" , DATE_FORMAT(@fromdate6 ,'%Y'), "', @ordertotalquantity7 '" , DATE_FORMAT(@fromdate7 ,'%Y'), "', @ordertotalquantity8 '" , DATE_FORMAT(@fromdate8 ,'%Y'), "', @ordertotalquantity9 '" , DATE_FORMAT(@fromdate9 ,'%Y'), "', @ordertotalquantity10 '" , DATE_FORMAT(@fromdate10 ,'%Y'), "' ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 ; 
       DEALLOCATE PREPARE stmt2;
    end if;
    
END  $$  $$

--  ------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getChart3Data`(
   IN frequency varchar(200)
)
BEGIN
   set @fromdate = '1000-01-01 00:00:00';
   set @todate = '9999-12-31 23:59:59';                      
   
   set @frequency = frequency;
   if frequency = '14days' then
	   set @todate = CURDATE() + INTERVAL 1 DAY; 
	   set @fromdate = DATE_SUB(@todate, INTERVAL 14 DAY);
   end if;   
   if frequency = '16weeks' then 
	   set @todate = (curdate()+interval 7 - weekday(curdate()) day);     
  	   set @fromdate = DATE_SUB(@todate, INTERVAL 16 WEEK);   
   end if;
  if frequency = '12months' then 
	   set @todate = adddate(last_day(curdate()), 1);     
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 11 month);           
   end if;
   if frequency = '10years' then 
 	   set @todate = CONCAT(YEAR(curdate())+1, '-01-01');       
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 9 year);
   end if;
 
   set @timezonehours = '+00:00';
   SET @statement = concat( 'Select value into @timezonehours from ', "(SELECT value FROM wp_dragonecommercezyx987_setting WHERE name = 'COMPANY_TIMEZONE_HOURS') a" ); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt; 
   if @timezonehours = '' or @timezonehours is null then 
       set @timezonehours = '+00:00';
   end if; 

   if frequency = '14days'  then 
       SET @statement = "SELECT min(p.name) 'name', sum(po.quantity * po.unitpriceexcltax) 'value' FROM wp_dragonecommercezyx987_product_order po join wp_dragonecommercezyx987_product p ON po.productid = p.id join wp_dragonecommercezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ? group by po.productid order by sum(po.quantity * po.unitpriceexcltax) desc, po.productid LIMIT 10"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate, @todate;
       DEALLOCATE PREPARE stmt; 
    end if;
    if frequency = '16weeks'  then        
       SET @statement = "SELECT min(p.name) 'name', sum(po.quantity * po.unitpriceexcltax) 'value' FROM wp_dragonecommercezyx987_product_order po join wp_dragonecommercezyx987_product p ON po.productid = p.id join wp_dragonecommercezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ? group by po.productid order by sum(po.quantity * po.unitpriceexcltax) desc, po.productid LIMIT 10";        
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;       
    if  frequency = '12months'  then 
       SET @statement = "SELECT min(p.name) 'name', sum(po.quantity * po.unitpriceexcltax) 'value' FROM wp_dragonecommercezyx987_product_order po join wp_dragonecommercezyx987_product p ON po.productid = p.id join wp_dragonecommercezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ? group by po.productid order by sum(po.quantity * po.unitpriceexcltax) desc, po.productid LIMIT 10";        
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
     end if;     
       
     if  frequency = '10years' then 
       SET @statement = "SELECT min(p.name) 'name', sum(po.quantity * po.unitpriceexcltax) 'value' FROM wp_dragonecommercezyx987_product_order po join wp_dragonecommercezyx987_product p ON po.productid = p.id join wp_dragonecommercezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ? group by po.productid order by sum(po.quantity * po.unitpriceexcltax) desc, po.productid LIMIT 10";        
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
       end if;	
END  $$  $$

--  -----------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getChart4Data`(
   IN frequency varchar(200)
)
BEGIN
   set @fromdate = '1000-01-01 00:00:00';
   set @todate = '9999-12-31 23:59:59';                      
   
   set @frequency = frequency;
   if frequency = '14days' then
	   set @todate = CURDATE() + INTERVAL 1 DAY; 
	   set @fromdate = DATE_SUB(@todate, INTERVAL 14 DAY);
   end if;   
   if frequency = '16weeks' then 
	   set @todate = (curdate()+interval 7 - weekday(curdate()) day);      
  	   set @fromdate = DATE_SUB(@todate, INTERVAL 16 WEEK);    
   end if;
  if frequency = '12months' then 
	   set @todate = adddate(last_day(curdate()), 1);     
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 11 month);           
   end if;
   if frequency = '10years' then 
 	   set @todate = CONCAT(YEAR(curdate())+1, '-01-01');       
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 9 year);
   end if;
 
   set @timezonehours = '+00:00';
   SET @statement = concat( 'Select value into @timezonehours from ', "(SELECT value FROM wp_dragonecommercezyx987_setting WHERE name = 'COMPANY_TIMEZONE_HOURS') a" ); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt; 
   if @timezonehours = '' or @timezonehours is null then 
       set @timezonehours = '+00:00';
   end if;
 
   SET @statement = "SELECT min(p.name) 'name', sum(po.quantity) 'value' FROM wp_dragonecommercezyx987_product_order po join wp_dragonecommercezyx987_product p ON po.productid = p.id join wp_dragonecommercezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(po.createtimeutc,'+00:00',@timezonehours) < ? group by po.productid order by sum(po.quantity) desc, sum(po.quantity * po.unitpriceexcltax) desc, po.productid LIMIT 10"; 
   PREPARE stmt FROM @statement;
   EXECUTE stmt using @fromdate, @todate;
   DEALLOCATE PREPARE stmt; 
 
END  $$  $$

--  -------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getChart5Data`(
   IN tablenameprefix varchar(200),
   IN frequency varchar(200)
)
BEGIN
   set @fromdate = '1000-01-01 00:00:00';
   set @todate = '9999-12-31 23:59:59';
                      
   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if; 
   
   set @frequency = frequency;
   if frequency = '14days' then
	   set @todate = CURDATE() + INTERVAL 1 DAY; 
	   set @fromdate = DATE_SUB(@todate, INTERVAL 14 DAY);
   end if;   
   if frequency = '16weeks' then 
	   set @todate = (curdate()+interval 7 - weekday(curdate()) day);       
  	   set @fromdate = DATE_SUB(@todate, INTERVAL 16 WEEK);     
   end if;
  if frequency = '12months' then 
	   set @todate = adddate(last_day(curdate()), 1);     
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL 11 month);            
   end if;
   if frequency = '10years' then 
 	   set @todate = CONCAT(YEAR(curdate())+1, '-01-01');      
  	   set @fromdate = DATE_SUB(DATE_FORMAT(NOW() ,'%Y-01-01'), INTERVAL 9 year);
   end if;
 
   set @timezonehours = '+00:00';
   SET @statement = concat( 'Select value into @timezonehours from ', "(SELECT value FROM wp_dragonecommercezyx987_setting WHERE name = 'COMPANY_TIMEZONE_HOURS') a" ); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt; 
   if @timezonehours = '' or @timezonehours is null then 
       set @timezonehours = '+00:00';
   end if;
 
   SET @statement = concat("SELECT min(u.user_login) 'name', sum(o.totalfee) 'value' FROM wp_dragonecommercezyx987_order o join ", @tablenameprefix, "users u on o.userid = u.ID where o.status IN (SELECT id FROM wp_dragonecommercezyx987_processstatus WHERE name IN ('ordered','shipped','received')) AND CONVERT_TZ(o.createtimeutc,'+00:00',@timezonehours) >= ? AND CONVERT_TZ(o.createtimeutc,'+00:00',@timezonehours) < ? group by o.userid order by sum(o.totalfee) desc, o.userid LIMIT 10"); 
   PREPARE stmt FROM @statement;
   EXECUTE stmt using @fromdate, @todate;
   DEALLOCATE PREPARE stmt; 

END  $$

--  -----------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getCityList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN countryid int,  
   IN provinceid int,    
   IN citystatus int,
   OUT citytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @countryid = countryid;
   set @provinceid = provinceid;
   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @citystatus = 0;
   set @citystatus2 = 1;
   if citystatus = -1 then set @citystatus = 0; set @citystatus2 = 1; end if;
   if citystatus = 0 then set @citystatus = 0; set @citystatus2 = 0; end if;
   if citystatus = 1 then set @citystatus = 1; set @citystatus2 = 1; end if;
   
   if searchwords = '' && countryid = 0 && provinceid = 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
     if searchwords = '' && countryid != 0 && provinceid = 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @countryid, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
    --  -------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    if searchwords = '' && countryid = 0 && provinceid != 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
     if searchwords = '' && countryid != 0 && provinceid != 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @countryid, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = concat( 'Select count(*) into @citytotalquantity from ', ' wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET citytotalquantity = @citytotalquantity;
   
--  ==========================================================================================================================   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid = 0  && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid'  from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.name DESC, c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'countryname' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'countryname' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid != 0 && provinceid = 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid != 0 && provinceid = 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
        if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid = 0  && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid'  from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid != 0 && provinceid = !0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by t.name ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc ASC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.name DESC, c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'countryname' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'countryname' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by t.name DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid != 0 && provinceid != 0 then SET @sqlv = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provinceid, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid != 0 && provinceid != 0 then SET @statement = "SELECT c.*, p.name 'provincename', t.name 'countryname', t.id 'countryid' from wp_dragonecommercezyx987_city c JOIN wp_dragonecommercezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercezyx987_country t ON p.countryid = t.id WHERE c.provinceid = ? AND c.name like ? AND p.countryid= ? AND c.status IN (?,?) order by c.createtimeutc DESC,c.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provinceid, @searchwords, @countryid, @citystatus, @citystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
END  $$

--  ---------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getCountryList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN countrystatus int,
   OUT countrytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @countrystatus = 0;
   set @countrystatus2 = 1;
   if countrystatus = -1 then set @countrystatus = 0; set @countrystatus2 = 1; end if;
   if countrystatus = 0 then set @countrystatus = 0; set @countrystatus2 = 0; end if;
   if countrystatus = 1 then set @countrystatus = 1; set @countrystatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @countrytotalquantity from ', ' wp_dragonecommercezyx987_country WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @countrystatus, @countrystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @countrytotalquantity from ', ' wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET countrytotalquantity = @countrytotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'abbreviation' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by abbreviation ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'abbreviation' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by abbreviation ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'abbreviation' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by abbreviation DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'abbreviation' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by abbreviation DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_country WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_country WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countrystatus, @countrystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  -----------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getCouponList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN searchshownname varchar(200),
   IN searchdescription varchar(200),
   IN fromdate date,
   IN todate date,  
   IN couponstatus int,
   OUT coupontotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchshownname = CONCAT('%', searchshownname, '%');
   set @searchdescription = CONCAT('%', searchdescription, '%');
   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
 
   set @fromdate = '1000-01-01 00:00:00';  -- fromdate;
   set @todate = '9999-12-31 23:59:59';  -- todate;
   if fromdate > '1000-01-01 00:00:00' then set @fromdate = fromdate; end if;
   if todate > '1000-01-01 00:00:00' && todate < '9999-12-31 23:59:59' then set @todate = todate; end if; 
   
   set @couponstatus = 0;
   set @couponstatus2 = 1;
   if couponstatus = -1 then set @couponstatus = 0; set @couponstatus2 = 1; end if;
   if couponstatus = 0 then set @couponstatus = 0; set @couponstatus2 = 0; end if;
   if couponstatus = 1 then set @couponstatus = 1; set @couponstatus2 = 1; end if;      
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################3

      if searchwords = '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @coupontotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET coupontotalquantity = @coupontotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    

    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
     
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
   --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
       --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
	--   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
	--   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
	--   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_COUPON' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @couponstatus, @couponstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 

END  $$

--  -----------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getCurrencyList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN currencystatus int,
   OUT currencytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @currencystatus = 0;
   set @currencystatus2 = 1;
   if currencystatus = -1 then set @currencystatus = 0; set @currencystatus2 = 1; end if;
   if currencystatus = 0 then set @currencystatus = 0; set @currencystatus2 = 0; end if;
   if currencystatus = 1 then set @currencystatus = 1; set @currencystatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @currencytotalquantity from ', ' wp_dragonecommercezyx987_currency WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @currencystatus, @currencystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @currencytotalquantity from ', ' wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET currencytotalquantity = @currencytotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_currency WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_currency WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @currencystatus, @currencystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  ------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getDepartmentList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN departmentstatus int,
   OUT departmenttotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @departmentstatus = 0;
   set @departmentstatus2 = 1;
   if departmentstatus = -1 then set @departmentstatus = 0; set @departmentstatus2 = 1; end if;
   if departmentstatus = 0 then set @departmentstatus = 0; set @departmentstatus2 = 0; end if;
   if departmentstatus = 1 then set @departmentstatus = 1; set @departmentstatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @departmenttotalquantity from ', ' wp_dragonecommercezyx987_department WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @departmentstatus, @departmentstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @departmenttotalquantity from ', ' wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET departmenttotalquantity = @departmenttotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_department WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_department WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @departmentstatus, @departmentstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  ---------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getDiscountList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN searchshownname varchar(200),
   IN searchdescription varchar(200),
   IN fromdate date,
   IN todate date,  
   IN discountstatus int,
   OUT discounttotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchshownname = CONCAT('%', searchshownname, '%');
   set @searchdescription = CONCAT('%', searchdescription, '%');
   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
 
   set @fromdate = '1000-01-01 00:00:00';  -- fromdate;
   set @todate = '9999-12-31 23:59:59';  -- todate;
   if fromdate > '1000-01-01 00:00:00' then set @fromdate = fromdate; end if;
   if todate > '1000-01-01 00:00:00' && todate < '9999-12-31 23:59:59' then set @todate = todate; end if; 
   
   set @discountstatus = 0;
   set @discountstatus2 = 1;
   if discountstatus = -1 then set @discountstatus = 0; set @discountstatus2 = 1; end if;
   if discountstatus = 0 then set @discountstatus = 0; set @discountstatus2 = 0; end if;
   if discountstatus = 1 then set @discountstatus = 1; set @discountstatus2 = 1; end if;
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################3

	if searchwords = '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @discounttotalquantity from ", " wp_dragonecommercezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET discounttotalquantity = @discounttotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    

    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
     
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
   --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
       --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc ASC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.name DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.shownname DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.description DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' then SET @sqlv = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchshownname, @searchdescription, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = "SELECT d.*,fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercezyx987_propertyvalue d LEFT JOIN wp_dragonecommercezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_DISCOUNT' and a.status = 1 LIMIT 1))) AND  d.shownname LIKE ? AND d.description like ? AND d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.createtimeutc DESC,d.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchdescription, @searchwords, @discountstatus, @discountstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 

END  $$

--  ----------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getDistanceList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200), 
   OUT distancetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
    SET @statement = concat( 'Select count(*) into @distancetotalquantity from ', ' wp_dragonecommercezyx987_distance' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt;
       DEALLOCATE PREPARE stmt;
         
   SET distancetotalquantity = @distancetotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    
     --   ---------------------------------------------
         if myorder = 'asc' && mysortby = 'name' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by name ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    
     --   ---------------------------------------------
    if myorder = 'asc' && mysortby = 'createtimeutc' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
     
    --   ---------------------------------------------
        if myorder = 'desc' && mysortby = 'name' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by name DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
     
    --   ---------------------------------------------
    if myorder = 'desc' && mysortby = 'createtimeutc' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_distance order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
END  $$

--  -----------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getEmailmethodList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200), 
   OUT emailmethodtotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
    SET @statement = concat( 'Select count(*) into @emailmethodtotalquantity from ', ' wp_dragonecommercezyx987_emailmethod' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt;
       DEALLOCATE PREPARE stmt;
         
   SET emailmethodtotalquantity = @emailmethodtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;

     --   ---------------------------------------------
    if myorder = 'asc' && mysortby = 'name' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by name ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;

     --   ---------------------------------------------
    if myorder = 'asc' && mysortby = 'createtimeutc' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;   
    
    --   ---------------------------------------------
    if myorder = 'desc' && mysortby = 'name' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by name DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;   
    
    --   ---------------------------------------------
    if myorder = 'desc' && mysortby = 'createtimeutc' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_emailmethod order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    
END  $$

--  ----------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getGroupcategoryList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN groupcategorystatus int,
   OUT groupcategorytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @groupcategorystatus = 0;
   set @groupcategorystatus2 = 1;
   if groupcategorystatus = -1 then set @groupcategorystatus = 0; set @groupcategorystatus2 = 1; end if;
   if groupcategorystatus = 0 then set @groupcategorystatus = 0; set @groupcategorystatus2 = 0; end if;
   if groupcategorystatus = 1 then set @groupcategorystatus = 1; set @groupcategorystatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @groupcategorytotalquantity from ', ' wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupcategorystatus, @groupcategorystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @groupcategorytotalquantity from ', ' wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET groupcategorytotalquantity = @groupcategorytotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by name DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercezyx987_groupcategory WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercezyx987_groupcategory WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategorystatus, @groupcategorystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  -----------------------------------------------
CREATE PROCEDURE `wp_dragonecommercezyx987_getGroupList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN groupcategoryname varchar(200),    
   IN groupstatus int,
   OUT grouptotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @groupcategoryname = CONCAT('%', groupcategoryname, '%');   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @groupstatus = 0;
   set @groupstatus2 = 1;
   if groupstatus = -1 then set @groupstatus = 0; set @groupstatus2 = 1; end if;
   if groupstatus = 0 then set @groupstatus = 0; set @groupstatus2 = 0; end if;
   if groupstatus = 1 then set @groupstatus = 1; set @groupstatus2 = 1; end if;
         
    if searchwords = '' and groupcategoryname = '' then SET @statement = concat( 'Select count(*) into @grouptotalquantity from ', ' wp_dragonecommercezyx987_group WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupstatus, @groupstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' and groupcategoryname = '' then SET @statement = concat( 'Select count(*) into @grouptotalquantity from ', ' wp_dragonecommercezyx987_group WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
	if searchwords = '' && groupcategoryname != '' then SET @statement = concat( 'Select count(*) into @grouptotalquantity from ', ' wp_dragonecommercezyx987_group p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupcategoryname, @groupstatus, @groupstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && groupcategoryname != '' then SET @statement = concat( 'Select count(*) into @grouptotalquantity from ', ' wp_dragonecommercezyx987_group p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
   SET grouptotalquantity = @grouptotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'groupcategoryname' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'groupcategoryname' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id  WHERE p.name like ? AND p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'groupcategoryname' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'groupcategoryname' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && groupcategoryname = '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && groupcategoryname = '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'groupcategoryname' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'groupcategoryname' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'groupcategoryname' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'groupcategoryname' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && groupcategoryname != '' then SET @sqlv = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE c.name LIKE ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && groupcategoryname != '' then SET @statement = "SELECT p.*, c.name 'groupcategoryname', c.id 'groupcategoryid' from wp_dragonecommercezyx987_group  p JOIN wp_dragonecommercezyx987_groupcategory c ON p.groupcategoryid = c.id WHERE p.name like ? AND c.name LIKE ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @groupcategoryname, @groupstatus, @groupstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
END  $$

--  --------------------------------------------------------
