DELIMITER $$
CREATE PROCEDURE `wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_order WHERE  status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po JOIN wp_dragonecommercereservezyx987_order o ON po.orderid = o.id WHERE  o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_product_order po join wp_dragonecommercereservezyx987_product p ON po.productid = p.id join wp_dragonecommercereservezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po join wp_dragonecommercereservezyx987_product p ON po.productid = p.id join wp_dragonecommercereservezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po join wp_dragonecommercereservezyx987_product p ON po.productid = p.id join wp_dragonecommercereservezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_product_order po join wp_dragonecommercereservezyx987_product p ON po.productid = p.id join wp_dragonecommercereservezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_product_order po join wp_dragonecommercereservezyx987_product p ON po.productid = p.id join wp_dragonecommercereservezyx987_order o ON po.orderid = o.id where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_order o join ", @tablenameprefix, "users u on o.userid = u.ID where o.status IN (SELECT id FROM wp_dragonecommercereservezyx987_processstatus WHERE name IN ('reserved','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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_city c JOIN wp_dragonecommercereservezyx987_province p ON c.provinceid = p.id JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_distance' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt;
       DEALLOCATE PREPARE stmt;
         
   SET distancetotalquantity = @distancetotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_emailmethod' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt;
       DEALLOCATE PREPARE stmt;
         
   SET emailmethodtotalquantity = @emailmethodtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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_dragonecommercereservezyx987_group  p JOIN wp_dragonecommercereservezyx987_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  $$

--  --------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getHandlingList`(
   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 handlingstatus int,
   OUT handlingtotalquantity 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 @handlingstatus = 0;
   set @handlingstatus2 = 1;
   if handlingstatus = -1 then set @handlingstatus = 0; set @handlingstatus2 = 1; end if;
   if handlingstatus = 0 then set @handlingstatus = 0; set @handlingstatus2 = 0; end if;
   if handlingstatus = 1 then set @handlingstatus = 1; set @handlingstatus2 = 1; end if;
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################3

      if searchwords = '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @handlingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET handlingtotalquantity = @handlingtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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 @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_HANDLING_DETAILS' 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, @handlingstatus, @handlingstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  ---------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getLogList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN searchtype varchar(200),
   IN processname varchar(200),
   IN userid int,      
   IN logstatus int,
   OUT logtotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchtype = searchtype;
   set @processname = processname;
   set @userid = userid;   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @logstatus = 0;
   set @logstatus2 = 1;
   if logstatus = -1 then set @logstatus = 0; set @logstatus2 = 1; end if;
   if logstatus = 0 then set @logstatus = 0; set @logstatus2 = 0; end if;
   if logstatus = 1 then set @logstatus = 1; set @logstatus2 = 1; end if;
   
    if searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
	if searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
    
    --   ####################################################################
 
     if searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
	if searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
    -- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	-- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   
	-- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	-- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         
	if searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
	if searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
    
    --   ####################################################################
 
	if searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
	if searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = concat( 'Select count(*) into @logtotalquantity from ', ' wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2;
       DEALLOCATE PREPARE stmt;
    end if;
         
   SET logtotalquantity = @logtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid <= 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid <= 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    --    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname = '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname = '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    -- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype = '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype = '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'type' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'type' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by `type` DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchtype != '' && processname != '' && userid > 0 then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @processname, @searchtype, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchtype != '' && processname != '' && userid > 0 then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_log WHERE userid = ? AND processname = ? AND `type` = ? AND description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @processname, @searchtype, @searchwords, @logstatus, @logstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
END  $$
--  -----------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getOrderdetailsList`(
   IN tablenameprefix varchar(200),
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN propertyvalueid int,    
   IN searchusername varchar(200), 
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),
   IN fromdate date,
   IN todate date,
   IN mystatus int,
   OUT ordertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if;
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @propertyvalueid = propertyvalueid;
   set @searchusername = CONCAT('%', searchusername, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  

   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 @mystatus = -1;
   if mystatus > 0  then set @mystatus = mystatus; end if;
--  --------------------------------------
set @querypart1 = "Select o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.createtimeutc) 'createtimeutc', ";
set @querypart1 =  concat(@querypart1 , "  GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription' "
," ,opv.propertyvalueid, group_concat(pv.shownname, '(', opv.propertyvalueid,')') 'propertyvalueshownnameandid',group_concat(p.name, '(', p.id,')') 'productnameandid',group_concat(pr.shownname, '(', pr.id,')') 'propertyshownnameandid' "
," from wp_dragonecommercereservezyx987_order o  "
," LEFT JOIN wp_dragonecommercereservezyx987_order_propertyvalue opv ON o.id=opv.orderid "
," LEFT JOIN wp_dragonecommercereservezyx987_propertyvalue pv ON opv.propertyvalueid = pv.id "
," LEFT JOIN wp_dragonecommercereservezyx987_property pr ON pv.propertyid = pr.id "
," LEFT JOIN wp_dragonecommercereservezyx987_product p ON opv.productid = p.id " 
," LEFT JOIN "
, @tablenameprefix
,"users u ON o.userid = u.ID "
," LEFT JOIN wp_dragonecommercereservezyx987_product_order po  ON opv.orderid = po.orderid and opv.productid = po.productid and opv.propertyvalueids = po.productpropertyvalueids "
);

--  ---------------------------------------
set @querypart2 =  concat(" ( Select o.id from wp_dragonecommercereservezyx987_order o  "
," LEFT JOIN wp_dragonecommercereservezyx987_order_propertyvalue opv ON o.id=opv.orderid "
," LEFT JOIN wp_dragonecommercereservezyx987_propertyvalue pv ON opv.propertyvalueid = pv.id "
," LEFT JOIN wp_dragonecommercereservezyx987_property pr ON pv.propertyid = pr.id "
," LEFT JOIN wp_dragonecommercereservezyx987_product p ON opv.productid = p.id " 
," LEFT JOIN "
, @tablenameprefix
,"users u ON o.userid = u.ID "
," LEFT JOIN wp_dragonecommercereservezyx987_product_order po  ON opv.orderid = po.orderid and opv.productid = po.productid and opv.propertyvalueids = po.productpropertyvalueids ");

set @querypart3 = "  group by o.id, opv.propertyvalueid ";
--  -----------------------------------------------------------

    if searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
    
	if searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  and u.user_login LIKE ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ---------------------------------------------------------------------------------------
   
    if searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus > 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
    
	if searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus > 0 && propertyvalueid <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
--  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
--  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   

    if searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus <= 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
	if searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  and u.user_login LIKE ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus <= 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ---------------------------------------------------------------------------------------
   
    if searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus > 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
    
	if searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  ' WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid ) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus > 0 && propertyvalueid > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', @querypart2,  " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  HAVING min(p.name) LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;

   SET ordertotalquantity = @ordertotalquantity;
--  =========================================================================================================================================================================================================================================
--  =========================================================================================================================================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0   then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus <= 0  && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0   then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0   then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id,orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  

    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVINGmin(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid <= 0   then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus > 0 && propertyvalueid <= 0   then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid <= 0   then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus > 0 && propertyvalueid <= 0   then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @sqlv = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid <= 0  then SET @statement = concat(@querypart1, " WHERE o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
--  ####################################################################################################################################################################################################################################################
--  ####################################################################################################################################################################################################################################################
--  ####################################################################################################################################################################################################################################################
--  ####################################################################################################################################################################################################################################################

    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus <= 0 && propertyvalueid > 0   then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus <= 0  && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus <= 0 && propertyvalueid > 0   then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus <= 0 && propertyvalueid > 0   then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id,orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  

    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  HAVING min(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVINGmin(p.name) like ? ORDER BY o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus > 0 && propertyvalueid > 0   then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus > 0 && propertyvalueid > 0   then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus > 0 && propertyvalueid > 0   then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus > 0 && propertyvalueid > 0   then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY o.id DESC, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY orderdetaildescription DESC,o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @sqlv = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid  ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0 && propertyvalueid > 0  then SET @statement = concat(@querypart1, " WHERE pv.id = ? and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? and u.user_login LIKE ? group by o.id, opv.propertyvalueid   HAVING min(p.name) like ? ORDER BY total DESC,o.id, orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvalueid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$

--  -----------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getOrderExportList`(
   IN tablenameprefix varchar(200),
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN searchusername varchar(200), 
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),
   IN fromdate date,
   IN todate date,
   IN mystatus int,
   OUT ordertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if;
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchusername = CONCAT('%', searchusername, '%');
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  

   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 @mystatus = -1;
   if mystatus > 0  then set @mystatus = mystatus; end if;

    if searchwords = '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
	if searchwords = '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ---------------------------------------------------------------------------------------
   
    if searchwords = '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
	if searchwords = '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
   SET ordertotalquantity = @ordertotalquantity;
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus <= 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus <= 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  

    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus > 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus > 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus > 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus > 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc',  o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC ");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC "); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
--  ------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getOrderList`(
   IN tablenameprefix varchar(200),
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN searchusername varchar(200), 
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),
   IN fromdate date,
   IN todate date,
   IN mystatus int,
   OUT ordertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if;
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchusername = CONCAT('%', searchusername, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  

   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 @mystatus = -1;
   if mystatus > 0  then set @mystatus = mystatus; end if;

    if searchwords = '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
	if searchwords = '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ---------------------------------------------------------------------------------------
   
    if searchwords = '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;
    
	if searchwords = '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
   SET ordertotalquantity = @ordertotalquantity;
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus <= 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus <= 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  

    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus > 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus > 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus > 0   then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus > 0   then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc',  o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.discountfee) 'discountfee',min(o.couponfee) 'couponfee',min(o.description) 'description', min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
--  --------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getPaymentmethodList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200), 
   OUT paymentmethodtotalquantity 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 @paymentmethodtotalquantity from ', ' wp_dragonecommercereservezyx987_paymentmethod' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt;
       DEALLOCATE PREPARE stmt;
         
   SET paymentmethodtotalquantity = @paymentmethodtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_paymentmethod 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_dragonecommercereservezyx987_getProcessstatusList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN processstatusstatus int,
   OUT processstatustotalquantity 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 @processstatusstatus = 0;
   set @processstatusstatus2 = 1;
   if processstatusstatus = -1 then set @processstatusstatus = 0; set @processstatusstatus2 = 1; end if;
   if processstatusstatus = 0 then set @processstatusstatus = 0; set @processstatusstatus2 = 0; end if;
   if processstatusstatus = 1 then set @processstatusstatus = 1; set @processstatusstatus2 = 1; end if;
   
    if searchwords = '' then SET @statement = concat( 'Select count(*) into @processstatustotalquantity from ', ' wp_dragonecommercereservezyx987_processstatus WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @processstatusstatus, @processstatusstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' then SET @statement = concat( 'Select count(*) into @processstatustotalquantity from ', ' wp_dragonecommercereservezyx987_processstatus WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @processstatusstatus, @processstatusstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET processstatustotalquantity = @processstatustotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

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

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_processstatus WHERE name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @processstatusstatus, @processstatusstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
--  -------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getProductdetailsList`(
   IN isonsale int,
   IN istaxfree int,            
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),  
   IN barcode varchar(200),
   IN departmentid int,   
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),  
   IN fromdate date,
   IN todate date,   
   IN productstatus int,
   OUT producttotalquantity int,
   OUT producthighestprice int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
 
   set @isonsale = 0;
   set @isonsale2 = 1;
   if isonsale = -1 then set @isonsale = 0; set @isonsale2 = 1; end if;
   if isonsale = 0 then set @isonsale = 0; set @isonsale2 = 0; end if;
   if isonsale = 1 then set @isonsale = 1; set @isonsale2 = 1; end if;
   
   set @istaxfree = 0;
   set @istaxfree2 = 1;
   if istaxfree = -1 then set @istaxfree = 0; set @istaxfree2 = 1; end if;
   if istaxfree = 0 then set @istaxfree = 0; set @istaxfree2 = 0; end if;
   if istaxfree = 1 then set @istaxfree = 1; set @istaxfree2 = 1; end if;
      
   set @searchwords = CONCAT('%', searchwords, '%');
   set @barcode = CONCAT('%', barcode, '%');
   set @departmentid = departmentid;
   
   set @pagenumber= pagenumber;
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  
 
   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 @productstatus = 1;
   if productstatus = 1 then set @productstatus = 1; end if;
   if productstatus = 0 then set @productstatus = 0; end if;

--   ----------------------------------------------------
	SET @statement = concat( 'Select CEIL(max(price)) into @producthighestprice from ', ' wp_dragonecommercereservezyx987_product' ); 
	PREPARE stmt FROM @statement;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
       
	SET producthighestprice = @producthighestprice;
	if maxprice < 0 then set @maxprice = @producthighestprice; end if;
--  --------------------------------------------------------
    set @querypart1 = "select a.id 'id',min(a.name) 'productname',min(b.name) 'departmentname', group_concat(p1.shownname ,'(',p1.id ,'): ',pv1.shownname ,'(',pv1.id ,')') 'propertyname_propertyvaluename', min(ppv.quantity) 'quantity', min(ppv.originalquantity) 'originalquantity', min(ppv.propertyvalueids) 'propertyvalueids', min(ppv.id) 'minppvid' ";	
	set @querypart1 =  concat(@querypart1 , " from wp_dragonecommercereservezyx987_product a   "
	 , " LEFT JOIN wp_dragonecommercereservezyx987_department b   "
	 , " on a.departmentid = b.id  ");
	set @querypart1 = concat(@querypart1 , " LEFT JOIN wp_dragonecommercereservezyx987_product_propertyvalue ppv  ");
	set @querypart1 = concat(@querypart1 , " on a.id = ppv.productid  ");
	set @querypart1 = concat(@querypart1 , " LEFT JOIN wp_dragonecommercereservezyx987_propertyvalue pv1  ");
	set @querypart1 = concat(@querypart1 , " on ppv.propertyvalueid = pv1.id  ");
	set @querypart1 = concat(@querypart1 , " LEFT JOIN wp_dragonecommercereservezyx987_property p1  ");
	set @querypart1 = concat(@querypart1 , " on pv1.propertyid = p1.id  ");                                                  

--  ------------------------------------------- 
	set @querypart2 = concat(" ( select a.id from  wp_dragonecommercereservezyx987_product a   "
	, " LEFT JOIN wp_dragonecommercereservezyx987_department b   "
	, " on a.departmentid = b.id  "
	, " LEFT JOIN wp_dragonecommercereservezyx987_product_propertyvalue ppv  "
	, " on a.id = ppv.productid  "
	, " LEFT JOIN wp_dragonecommercereservezyx987_propertyvalue pv1  "
	, " on ppv.propertyvalueid = pv1.id  "
	, " LEFT JOIN wp_dragonecommercereservezyx987_property p1 "
	, " on pv1.propertyid = p1.id  "); 

    set @querypart3 = " group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null ";
--  -----------------------------------------------------------
   
    if searchwords = '' && departmentid = 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from ', @querypart2, '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid = 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ', @querypart2,  '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.name like ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null  ) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if searchwords = '' && departmentid != 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ', @querypart2,  '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.departmentid = ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null  ) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid != 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ', @querypart2,  '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.name like ? AND a.departmentid = ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null  ) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;
	
	if searchwords = '' && departmentid = 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ',  @querypart2, '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ? and a.barcode LIKE ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null  ) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid = 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ',  @querypart2, '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.name like ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ? and a.barcode LIKE ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null  ) z ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if searchwords = '' && departmentid != 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ',  @querypart2, '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.departmentid = ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ? and a.barcode LIKE ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null ) z  ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid != 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity  from  ',  @querypart2, '  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.name like ? AND a.departmentid = ? AND a.status = ? and a.price >= ? and a.price <= ? and a.createtimeutc >= ? and a.createtimeutc <= ? and a.barcode LIKE ?  group by a.id, trim(ppv.propertyvalueids) having min(ppv.propertyvalueids) is not null ) z  ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;

   SET producttotalquantity = @producttotalquantity;
   
   if producttotalquantity > 0 && amountperpage > 0 &&  pagenumber > CEIL(producttotalquantity/amountperpage) then set @myoffset = (CEIL(producttotalquantity/amountperpage) - 1) * @amountperpage;  end if;
--  ==========================================================================================================================
--  ==========================================================================================================================

    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement =  concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement =  concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement =  concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;

    --   ---------------------------------------------
    
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,a.name,a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1," WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,a.name,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,a.name,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,a.name,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
 
     --   ---------------------------------------------

    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1,"  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
 
     if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
    
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;	
	
	--   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id ASC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
	
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) ASC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
 
     --   ---------------------------------------------

    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) ASC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by a.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
    
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.name) DESC,a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(ifnull(a.saleprice, a.price)) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;	
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = concat(@querypart1, "  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = concat(@querypart1, " WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ?  " ,  @querypart3, " order by min(a.createtimeutc) DESC,min(a.name),a.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
   
END  $$
--  ------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getProductList`(
   IN isonsale int,
   IN istaxfree int,            
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),  
   IN barcode varchar(200),
   IN departmentid int,   
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),  
   IN fromdate date,
   IN todate date,   
   IN productstatus int,
   OUT producttotalquantity int,
   OUT producthighestprice int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
 
   set @isonsale = 0;
   set @isonsale2 = 1;
   if isonsale = -1 then set @isonsale = 0; set @isonsale2 = 1; end if;
   if isonsale = 0 then set @isonsale = 0; set @isonsale2 = 0; end if;
   if isonsale = 1 then set @isonsale = 1; set @isonsale2 = 1; end if;
   
   set @istaxfree = 0;
   set @istaxfree2 = 1;
   if istaxfree = -1 then set @istaxfree = 0; set @istaxfree2 = 1; end if;
   if istaxfree = 0 then set @istaxfree = 0; set @istaxfree2 = 0; end if;
   if istaxfree = 1 then set @istaxfree = 1; set @istaxfree2 = 1; end if;
      
   set @searchwords = CONCAT('%', searchwords, '%');
   set @barcode = CONCAT('%', barcode, '%');
   set @departmentid = departmentid;
   
   set @pagenumber= pagenumber;
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  
 
   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 @productstatus = 1;
   if productstatus = 1 then set @productstatus = 1; end if;
   if productstatus = 0 then set @productstatus = 0; end if;

--   ----------------------------------------------------
	SET @statement = concat( 'Select CEIL(max(price)) into @producthighestprice from ', ' wp_dragonecommercereservezyx987_product' ); 
	PREPARE stmt FROM @statement;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
       
	SET producthighestprice = @producthighestprice;
	if maxprice < 0 then set @maxprice = @producthighestprice; end if;  
--  --------------------------------------------------------
   
    if searchwords = '' && departmentid = 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid = 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and name like ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if searchwords = '' && departmentid != 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and departmentid = ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid != 0 && barcode = '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and name like ? AND departmentid = ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? ' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;
	
	if searchwords = '' && departmentid = 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? and barcode LIKE ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid = 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and name like ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? and barcode LIKE ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if searchwords = '' && departmentid != 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and departmentid = ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? and barcode LIKE ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && departmentid != 0 && barcode != '' then SET @statement = concat( 'Select count(*) into @producttotalquantity from ', ' wp_dragonecommercereservezyx987_product WHERE isonsale IN (?,?) and istaxfree IN (?,?) and name like ? AND departmentid = ? AND status = ? and price >= ? and price <= ? and createtimeutc >= ? and createtimeutc <= ? and barcode LIKE ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @searchwords, @departmentid, @productstatus, @minprice, @maxprice, @fromdate, @todate, @barcode;
       DEALLOCATE PREPARE stmt;    
    end if;
	
   SET producttotalquantity = @producttotalquantity;
   
   if producttotalquantity > 0 && amountperpage > 0 &&  pagenumber > CEIL(producttotalquantity/amountperpage) then set @myoffset = (CEIL(producttotalquantity/amountperpage) - 1) * @amountperpage;  end if;
--  ==========================================================================================================================
--  ==========================================================================================================================

    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
    
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.pric,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
 
     --   ---------------------------------------------

    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
 
     if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
    
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode = '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;	
	
	--   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
	
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.name ASC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
 
     --   ---------------------------------------------

    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc ASC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    --  ===================================================================  BEGIN OF ORDER DESC ========================================

    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'id' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.id DESC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------
    
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'name' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.name DESC,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'price' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by ifnull(a.saleprice, a.price) DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;	
    
    --   ---------------------------------------------

    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid = 0 then SET @sqlv = "SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id  WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid = 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if barcode != '' && myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && departmentid != 0 then SET @statement = 'SELECT a.onsalestartdateutc,a.onsaleenddateutc,a.id,a.quantity,a.originalquantity,a.departmentid,a.name, a.description,a.imagefilenames,a.weightlb,a.price,a.saleprice,a.isonsale,a.istaxfree,a.barcode,a.createtimeutc,b.name as departmentname from wp_dragonecommercereservezyx987_product a LEFT JOIN wp_dragonecommercereservezyx987_department b on a.departmentid = b.id WHERE a.isonsale IN (?,?) and a.istaxfree IN (?,?) and a.barcode LIKE ? AND a.createtimeutc >= ? AND a.createtimeutc <= ? AND a.price >= ? AND a.price <= ? AND a.name like ? AND a.departmentid = ? AND a.status = ? order by a.createtimeutc DESC,a.name,a.id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @isonsale, @isonsale2, @istaxfree, @istaxfree2, @barcode, @fromdate, @todate, @minprice, @maxprice, @searchwords, @departmentid, @productstatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
END  $$
--  --------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getPropertyList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN chargetypeid int,    
   IN propertystatus int,
   OUT propertytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @chargetypeid = chargetypeid;
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @propertystatus = 0;
   set @propertystatus2 = 1;
   if propertystatus = -1 then set @propertystatus = 0; set @propertystatus2 = 1; end if;
   if propertystatus = 0 then set @propertystatus = 0; set @propertystatus2 = 0; end if;
   if propertystatus = 1 then set @propertystatus = 1; set @propertystatus2 = 1; end if;
   
    if searchwords = '' and chargetypeid = 0 then SET @statement = concat( 'Select count(*) into @propertytotalquantity from ', ' wp_dragonecommercereservezyx987_property WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertystatus, @propertystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' and chargetypeid = 0 then SET @statement = concat( 'Select count(*) into @propertytotalquantity from ', ' wp_dragonecommercereservezyx987_property WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @propertystatus, @propertystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
     if searchwords = '' && chargetypeid != 0 then SET @statement = concat( 'Select count(*) into @propertytotalquantity from ', ' wp_dragonecommercereservezyx987_property p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.chargetypeid= ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @chargetypeid, @propertystatus, @propertystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && chargetypeid != 0 then SET @statement = concat( 'Select count(*) into @propertytotalquantity from ', ' wp_dragonecommercereservezyx987_property p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.name like ? AND p.chargetypeid= ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypeid, @propertystatus, @propertystatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
   SET propertytotalquantity = @propertytotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'chargetypename' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'chargetypename' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'chargetypename' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'chargetypename' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && chargetypeid = 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && chargetypeid = 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = 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, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && chargetypeid != 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.chargetypeid= ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && chargetypeid != 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.name like ? AND p.chargetypeid= ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && chargetypeid != 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.chargetypeid= ? AND p.status IN (?,?) order by p.name ASC ,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && chargetypeid != 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.name like ? AND p.chargetypeid= ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'chargetypename' && searchwords = '' && chargetypeid != 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.chargetypeid= ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'chargetypename' && searchwords != '' && chargetypeid != 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.name like ? AND p.chargetypeid= ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

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

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && chargetypeid != 0 then SET @sqlv = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.chargetypeid= ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && chargetypeid != 0 then SET @statement = "SELECT p.*, c.name 'chargetypename' from wp_dragonecommercereservezyx987_property  p LEFT JOIN wp_dragonecommercereservezyx987_chargetype c ON p.chargetypeid = c.id WHERE p.name like ? AND p.chargetypeid= ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @chargetypeid, @propertystatus, @propertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
END  $$
--  -------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getGroupPropertyList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN groupid int,   
   IN propertyid int,    
   IN grouppropertystatus int,
   OUT grouppropertytotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @groupid = groupid;
   set @propertyid = propertyid;
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   
   set @grouppropertystatus = 0;
   set @grouppropertystatus2 = 1;
   if grouppropertystatus = -1 then set @grouppropertystatus = 0; set @grouppropertystatus2 = 1; end if;
   if grouppropertystatus = 0 then set @grouppropertystatus = 0; set @grouppropertystatus2 = 0; end if;
   if grouppropertystatus = 1 then set @grouppropertystatus = 1; set @grouppropertystatus2 = 1; end if;
   
    if groupid = 0 and propertyid = 0 then SET @statement = concat( 'Select count(*) into @grouppropertytotalquantity from ', ' wp_dragonecommercereservezyx987_group_property WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @grouppropertystatus, @grouppropertystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if groupid != 0 and propertyid = 0 then SET @statement = concat( 'Select count(*) into @grouppropertytotalquantity from ', ' wp_dragonecommercereservezyx987_group_property WHERE groupid = ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
     if groupid = 0 && propertyid != 0 then SET @statement = concat( 'Select count(*) into @grouppropertytotalquantity from ', ' wp_dragonecommercereservezyx987_group_property p WHERE p.propertyid= ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyid, @grouppropertystatus, @grouppropertystatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if groupid != 0 && propertyid != 0 then SET @statement = concat( 'Select count(*) into @grouppropertytotalquantity from ', ' wp_dragonecommercereservezyx987_group_property p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
   SET grouppropertytotalquantity = @grouppropertytotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
    if myorder = 'asc' && mysortby = 'id' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'groupid' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.groupid ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'groupid' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.groupid ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'propertyid' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.propertyid ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'propertyid' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.propertyid ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
     if myorder = 'desc' && mysortby = 'id' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'groupid' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.groupid DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'groupid' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.groupid DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'propertyid' && groupid = 0 && propertyid = 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.status IN (?,?) order by p.propertyid DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'propertyid' && groupid != 0 && propertyid = 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.status IN (?,?) order by p.propertyid DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 	if myorder = 'asc' && mysortby = 'id' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.*  from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
	if myorder = 'asc' && mysortby = 'groupid' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.groupid ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'groupid' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.*  from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.groupid ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'propertyid' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.propertyid ASC ,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'propertyid' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.propertyid ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'groupid' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.groupid DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'groupid' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.groupid DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'propertyid' && groupid = 0 && propertyid != 0 then SET @sqlv = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE p.propertyid= ? AND p.status IN (?,?) order by p.propertyid DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'propertyid' && groupid != 0 && propertyid != 0 then SET @statement = "SELECT p.* from wp_dragonecommercereservezyx987_group_property  p WHERE  p.groupid = ?  AND p.propertyid= ? AND p.status IN (?,?) order by p.propertyid DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @groupid, @propertyid, @grouppropertystatus, @grouppropertystatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
END  $$
--  -------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getPropertyvalueList`(
   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 propertyvaluestatus int,
   IN propertyid int,
   OUT propertyvaluetotalquantity 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 @propertyvaluestatus = 0;
   set @propertyvaluestatus2 = 1;
   if propertyvaluestatus = -1 then set @propertyvaluestatus = 0; set @propertyvaluestatus2 = 1; end if;
   if propertyvaluestatus = 0 then set @propertyvaluestatus = 0; set @propertyvaluestatus2 = 0; end if;
   if propertyvaluestatus = 1 then set @propertyvaluestatus = 1; set @propertyvaluestatus2 = 1; end if;
   
   set @propertyid = propertyid;
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE 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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################

      if searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE 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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE 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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE 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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
     if searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? AND  d.name like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? AND  d.description like ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################

      if searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? AND   d.shownname LIKE ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0  then SET @statement = concat( "Select count(*) into @propertyvaluetotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET propertyvaluetotalquantity = @propertyvaluetotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0  then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    

    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
     
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
   --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
       --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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 @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid <= 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   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, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ********************************************************************************************************************************************************
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     --  ********************************************************************************************************************************************************
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0  then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id WHERE d.propertyid = ? AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    

    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;      
     --   ---------------------------------------------
     
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? AND  d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ?  order by d.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
   --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
       --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname = '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription = '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
        --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
            --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
     --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
     
         --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'shownname' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'shownname' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
             --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE  d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @sqlv = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' && searchshownname != '' && propertyid > 0 then SET @statement = "SELECT d.*,p.name 'propertyname',fp.name 'fromprovince',fc.name 'fromcountry',tp.name 'toprovince',tc.name 'tocountry' FROM wp_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_property p ON d.propertyid = p.id  LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE   d.propertyid = ? 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 @propertyid, @searchshownname, @searchdescription, @searchwords, @propertyvaluestatus, @propertyvaluestatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
END  $$
--  ------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getProvinceList`(
   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 provincestatus int,
   OUT provincetotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @countryid = countryid; -- CONCAT('%', countryname, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;    
   set @provincestatus = 0;
   set @provincestatus2 = 1;
   if provincestatus = -1 then set @provincestatus = 0; set @provincestatus2 = 1; end if;
   if provincestatus = 0 then set @provincestatus = 0; set @provincestatus2 = 0; end if;
   if provincestatus = 1 then set @provincestatus = 1; set @provincestatus2 = 1; end if;
   
    if searchwords = '' and countryid = 0 then SET @statement = concat( 'Select count(*) into @provincetotalquantity from ', ' wp_dragonecommercereservezyx987_province WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @provincestatus, @provincestatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' and countryid = 0 then SET @statement = concat( 'Select count(*) into @provincetotalquantity from ', ' wp_dragonecommercereservezyx987_province WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @provincestatus, @provincestatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
 
    --  ---------------------
 
     if searchwords = '' && countryid != 0 then SET @statement = concat( 'Select count(*) into @provincetotalquantity from ', ' wp_dragonecommercereservezyx987_province p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.countryid = ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @countryid, @provincestatus, @provincestatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && countryid != 0 then SET @statement = concat( 'Select count(*) into @provincetotalquantity from ', ' wp_dragonecommercereservezyx987_province p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.name like ? AND p.countryid = ? AND p.status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @provincestatus, @provincestatus2;
       DEALLOCATE PREPARE stmt;
    end if; 
    
   SET provincetotalquantity = @provincetotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.createtimeutc ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.id DESC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'name' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'name' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'countryname' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by c.name DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'countryname' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid = 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid = 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = 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, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    
    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && countryid != 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.countryid = ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && countryid != 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.name like ? AND p.countryid = ? AND p.status IN (?,?) order by p.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && countryid != 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.countryid = ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && countryid != 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.name like ? AND p.countryid = ? AND p.status IN (?,?) order by p.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'countryname' && searchwords = '' && countryid != 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.countryid = ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'countryname' && searchwords != '' && countryid != 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.name like ? AND p.countryid = ? AND p.status IN (?,?) order by c.name ASC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

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

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && countryid != 0 then SET @sqlv = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.countryid = ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && countryid != 0 then SET @statement = "SELECT p.*, c.name 'countryname' from wp_dragonecommercereservezyx987_province  p JOIN wp_dragonecommercereservezyx987_country c ON p.countryid = c.id WHERE p.name like ? AND p.countryid = ? AND p.status IN (?,?) order by p.createtimeutc DESC,p.id LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @countryid, @provincestatus, @provincestatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    
END  $$
--  ------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getShippingList`(
   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 shippingstatus int,
   OUT shippingtotalquantity 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 @shippingstatus = 0;
   set @shippingstatus2 = 1;
   if shippingstatus = -1 then set @shippingstatus = 0; set @shippingstatus2 = 1; end if;
   if shippingstatus = 0 then set @shippingstatus = 0; set @shippingstatus2 = 0; end if;
   if shippingstatus = 1 then set @shippingstatus = 1; set @shippingstatus2 = 1; end if;
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################3

	if searchwords = '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @shippingtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET shippingtotalquantity = @shippingtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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 @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_SHIPPING_DETAILS' 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, @shippingstatus, @shippingstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
END  $$
--  --------------------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getSlideshowList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),   
   IN searchdescription varchar(200),
   IN slideshowstatus int,
   OUT slideshowtotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchdescription = CONCAT('%', searchdescription, '%');   
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @slideshowstatus = 0;
   set @slideshowstatus2 = 1;
   if slideshowstatus = -1 then set @slideshowstatus = 0; set @slideshowstatus2 = 1; end if;
   if slideshowstatus = 0 then set @slideshowstatus = 0; set @slideshowstatus2 = 0; end if;
   if slideshowstatus = 1 then set @slideshowstatus = 1; set @slideshowstatus2 = 1; end if;
   
    if searchwords = '' && searchdescription = '' then SET @statement = concat( 'Select count(*) into @slideshowtotalquantity from ', ' wp_dragonecommercereservezyx987_slideshow WHERE status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @slideshowstatus, @slideshowstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' then SET @statement = concat( 'Select count(*) into @slideshowtotalquantity from ', ' wp_dragonecommercereservezyx987_slideshow WHERE name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @slideshowstatus, @slideshowstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
        if searchwords = '' && searchdescription != '' then SET @statement = concat( 'Select count(*) into @slideshowtotalquantity from ', ' wp_dragonecommercereservezyx987_slideshow WHERE description like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @slideshowstatus, @slideshowstatus2;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' then SET @statement = concat( 'Select count(*) into @slideshowtotalquantity from ', ' wp_dragonecommercereservezyx987_slideshow WHERE description like ? AND name like ? AND status IN (?,?)' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @slideshowstatus, @slideshowstatus2;
       DEALLOCATE PREPARE stmt;
    end if;  
    
   SET slideshowtotalquantity = @slideshowtotalquantity;
   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchdescription = '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE status IN (?,?) order by id ASC LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && searchdescription = '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE name like ? AND status IN (?,?) order by id ASC LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'name' && searchwords = '' && searchdescription = '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'name' && searchwords != '' && searchdescription = '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE name like ? AND status IN (?,?) order by name ASC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------

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

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

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

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && searchdescription != '' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE description like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @searchdescription, @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && searchdescription != '' then SET @statement = 'SELECT * from wp_dragonecommercereservezyx987_slideshow WHERE description like ? AND name like ? AND status IN (?,?) order by createtimeutc DESC,id LIMIT ? OFFSET ?'; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchdescription, @searchwords, @slideshowstatus, @slideshowstatus2, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
END  $$
--  ------------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getTaxList`(
   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 taxstatus int,
   OUT taxtotalquantity 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 @taxstatus = 0;
   set @taxstatus2 = 1;
   if taxstatus = -1 then set @taxstatus = 0; set @taxstatus2 = 1; end if;
   if taxstatus = 0 then set @taxstatus = 0; set @taxstatus2 = 0; end if;
   if taxstatus = 1 then set @taxstatus = 1; set @taxstatus2 = 1; end if;
   
    if searchwords = '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' and a.status = 1 LIMIT 1))) AND d.status IN (?,?) and d.createtimeutc >= ? and d.createtimeutc <= ? " ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname = '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
--  ##########################################################################3

      if searchwords = '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription = '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;  
    
    --  -------------------------
    if searchwords = '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchdescription != '' && searchshownname != '' then SET @statement = concat( "Select count(*) into @taxtotalquantity from ", " wp_dragonecommercereservezyx987_propertyvalue d WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;
    
   SET taxtotalquantity = @taxtotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================
 
    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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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 @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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,d.id ASC LIMIT ? OFFSET ?"; 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchshownname, @searchwords, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @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_dragonecommercereservezyx987_propertyvalue d LEFT JOIN wp_dragonecommercereservezyx987_province fp ON d.fromstateprovinceid = fp.id  LEFT JOIN wp_dragonecommercereservezyx987_province tp ON d.tostateprovinceid = tp.id LEFT JOIN wp_dragonecommercereservezyx987_country fc ON d.fromcountryid = fc.id  LEFT JOIN wp_dragonecommercereservezyx987_country tc ON d.tocountryid = tc.id  WHERE d.propertyid IN (SELECT c.propertyid FROM wp_dragonecommercereservezyx987_group_property c WHERE c.groupid IN (SELECT b.id FROM wp_dragonecommercereservezyx987_group b WHERE b.groupcategoryid = (SELECT CAST(a.value AS UNSIGNED) FROM wp_dragonecommercereservezyx987_setting a WHERE a.name = 'GROUP_CATEGORY_ID_FOR_ORDER_TAX_DETAILS' 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, @taxstatus, @taxstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
END  $$
--  ------------------------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getUserList`(
   IN tablenameprefix varchar(200),
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),  
   IN email varchar(200),
   IN fromdate date,
   IN todate date,   
   IN userstatus int,
   OUT usertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';
 
   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if;   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @email = CONCAT('%', email, '%');
   
   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 @userstatus = 0;
   set @userstatus2 = 1;
   if userstatus = -1 then set @userstatus = 0; set @userstatus2 = 1; end if;
   if userstatus = 0 then set @userstatus = 0; set @userstatus2 = 0; end if;
   if userstatus = 1 then set @userstatus = 1; set @userstatus2 = 1; end if;
   
    if searchwords = '' && email = '' then SET @statement = concat( 'Select count(*) into @usertotalquantity from ', ' wp_dragonecommercereservezyx987_user_status WHERE user_status IN (?,?) and createtimeutc >= ? and createtimeutc <= ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userstatus, @userstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && email = '' then SET @statement = concat( 'Select count(*) into @usertotalquantity from ', ' wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;    

    if searchwords = '' && email != '' then SET @statement = concat( 'Select count(*) into @usertotalquantity from ', ' wp_dragonecommercereservezyx987_user_status  a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_email like ? and a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @email, @userstatus, @userstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && email != '' then SET @statement = concat( 'Select count(*) into @usertotalquantity from ', ' wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_email like ? and b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ?' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @email, @searchwords, @userstatus, @userstatus2, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;
    end if;   
    
   SET usertotalquantity = @usertotalquantity;
   
--  ==========================================================================================================================   
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by a.userid ASC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by a.userid ASC LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'username' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_login ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'username' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_login ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'email' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_email ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'email' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_email ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID  WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_registered ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_registered ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
     if myorder = 'desc' && mysortby = 'id' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by a.userid DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by a.userid DESC LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   ---------------------------------------------
    
   if myorder = 'desc' && mysortby = 'username' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_login DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'username' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_login DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
   if myorder = 'desc' && mysortby = 'email' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_email DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'email' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_email DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && email = '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID  WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_registered DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && email = '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? order by b.user_registered DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    

    --   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by a.userid ASC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by a.userid ASC LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'username' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_login ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'username' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_login ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email,  @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'email' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_email ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'email' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_email ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;     
    --   ---------------------------------------------

    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID  WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_registered ASC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'createtimeutc' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_registered ASC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
     if myorder = 'desc' && mysortby = 'id' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by a.userid DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE b.user_login like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by a.userid DESC LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    --   ---------------------------------------------
    
   if myorder = 'desc' && mysortby = 'username' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_login DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'username' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_login DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
     --   ---------------------------------------------
    
   if myorder = 'desc' && mysortby = 'email' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_email DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'email' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_email DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    --   ---------------------------------------------

    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords = '' && email != '' then SET @sqlv = concat("SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ", @tablenameprefix, "users b on a.userid = b.ID  WHERE a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_registered DESC,a.userid LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'createtimeutc' && searchwords != '' && email != '' then SET @statement = concat('SELECT a.*,b.user_login,b.user_email,b.user_registered from wp_dragonecommercereservezyx987_user_status a LEFT JOIN ', @tablenameprefix, 'users b on a.userid = b.ID WHERE a.name like ? AND a.user_status IN (?,?) and a.createtimeutc >= ? and a.createtimeutc <= ? and b.user_email LIKE ? order by b.user_registered DESC,a.userid LIMIT ? OFFSET ?'); 
       PREPARE stmt FROM @statement;
       set @searchwords = CONCAT('%', searchwords, '%');
       EXECUTE stmt using @searchwords, @userstatus, @userstatus2, @fromdate, @todate, @email, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
END  $$
--  ------------------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getUserOrderList`(
   IN tablenameprefix varchar(200),
   IN userid int,
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200),  
   IN searchwords varchar(200),
   IN searchusername varchar(200), 
   IN minprice DECIMAL(15,2),
   IN maxprice DECIMAL(15,2),
   IN fromdate date,
   IN todate date,
   IN mystatus int,
   OUT ordertotalquantity int
)
BEGIN
   declare total int default 0;
   declare whereclause varchar(255) default ''; 
   declare orderbyclause varchar(10000) default '';

   set @userid = userid;
   set @tablenameprefix = tablenameprefix;
   if tablenameprefix = '' then set @tablenameprefix = 'wp_'; end if;
   
   set @searchwords = CONCAT('%', searchwords, '%');
   set @searchusername = CONCAT('%', searchusername, '%');
   set @amountperpage = amountperpage;
   set @myoffset = myoffset;
   set @minprice = 0;  -- minprice;
   set @maxprice = 9999999999999;  -- maxprice;
   if minprice > 0 then set @minprice = minprice; end if;
   if maxprice > 0 then set @maxprice = maxprice; end if;  

   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 @mystatus = -1;
   if mystatus > 0  then set @mystatus = mystatus; end if;

   if searchwords = '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
	if searchwords = '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus <= 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --  ---------------------------------------------------------------------------------------
   
    if searchwords = '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername = '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id group by o.id HAVING names LIKE ? ) a" ); 
       PREPARE stmt FROM @statement;       
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if;   
    
    
	if searchwords = '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', '(Select count(*) from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ', @tablenameprefix, 'users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id) a' ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername;
       DEALLOCATE PREPARE stmt;    
    end if;            
    if searchwords != '' && searchusername != '' && mystatus > 0 then SET @statement = concat( 'Select count(*) into @ordertotalquantity from ', "(Select count(*), group_concat(p.name, '|') 'names' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id HAVING names LIKE ?) a" ); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
   SET ordertotalquantity = @ordertotalquantity;
--  ==========================================================================================================================
--  ==========================================================================================================================
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ?  JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus <= 0   then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus <= 0   then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus <= 0   then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus <= 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus <= 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  

    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    --   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
	if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username'  from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;    
    
    if myorder = 'asc' && mysortby = 'id' && searchwords = '' && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'id' && searchwords != ''&& searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername = '' && mystatus > 0   then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername = '' && mystatus > 0   then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
    if myorder = 'asc' && mysortby = 'description' && searchwords = '' && searchusername != '' && mystatus > 0   then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'description' && searchwords != '' && searchusername != '' && mystatus > 0   then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
     --   ---------------------------------------------   

    if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    
   if myorder = 'asc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'asc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;  
    
    --  ===================================================================  BEGIN OF ORDER DESC ========================================
    
    if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'id' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY o.id DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'id' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY o.id DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------
    
    if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
   
   if myorder = 'desc' && mysortby = 'description' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'description' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY orderdetaildescription DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if; 
    
    --   ---------------------------------------------    
    
    if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername = '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @amountperpage, @myoffset; 
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername = '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
    
   if myorder = 'desc' && mysortby = 'total' && searchwords = ''  && searchusername != '' && mystatus > 0  then SET @sqlv = concat("Select min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id  JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id ORDER BY total DESC LIMIT ? OFFSET ?");
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
    if myorder = 'desc' && mysortby = 'total' && searchwords != ''  && searchusername != '' && mystatus > 0  then SET @statement = concat("Select  min(o.emailsent) 'emailsent', min(o.ordercancelemailsent) 'ordercancelemailsent', min(o.ordercanceltimeutc) 'ordercanceltimeutc', min(o.ordercancelrefunded) 'ordercancelrefunded', min(o.ordercancelrefundtimeutc) 'ordercancelrefundtimeutc', group_concat(p.name, '|') 'names', o.id, min(o.status) 'status', min(o.totalfee) 'total', min(o.subtotal) 'subtotal', min(o.discountdescriptionforuser) 'discount', min(o.coupondescriptionforuser) 'coupon', min(o.shippingfee) 'shippingfee', min(o.handlingfee) 'handlingfee', min(o.tax) 'tax', min(o.createtimeutc) 'createtimeutc', GROUP_CONCAT(CONCAT('ID: ', p.id, ', Name: ', p.name, ', Price: ', po.unitpriceexcltax, ', Quantity: ', po.quantity, ', Details: ', po.productdescription) SEPARATOR '; ') 'orderdetaildescription', min(u.user_login) 'username' from wp_dragonecommercereservezyx987_order o JOIN wp_dragonecommercereservezyx987_product_order po ON o.id=po.orderid and  o.userid = ? AND o.totalfee >= ? and o.totalfee <= ? and o.createtimeutc >= ? and o.createtimeutc <= ? and o.status = ? JOIN wp_dragonecommercereservezyx987_product p ON po.productid = p.id JOIN ", @tablenameprefix, "users u ON o.userid = u.ID and u.user_login LIKE ? group by o.id  HAVING names like ? ORDER BY total DESC LIMIT ? OFFSET ?"); 
       PREPARE stmt FROM @statement;
       EXECUTE stmt using @userid, @minprice, @maxprice, @fromdate, @todate, @mystatus, @searchusername, @searchwords, @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt;    
    end if;
END  $$

--  -----------------------------------------------------------------
CREATE PROCEDURE `wp_dragonecommercereservezyx987_getWeightList`(
   IN pagenumber int,
   IN amountperpage int,
   IN myoffset int, 
   IN mysortby varchar(200),
   IN myorder varchar(200), 
   OUT weighttotalquantity 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 @weighttotalquantity from ', ' wp_dragonecommercereservezyx987_weight' ); 
	PREPARE stmt FROM @statement;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
         
   SET weighttotalquantity = @weighttotalquantity;
   
--  ==========================================================================================================================
--  ==========================================================================================================================    
    if myorder = 'asc' && mysortby = 'id' then SET @sqlv = "SELECT * from wp_dragonecommercereservezyx987_weight 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_dragonecommercereservezyx987_weight 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_dragonecommercereservezyx987_weight 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_dragonecommercereservezyx987_weight 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_dragonecommercereservezyx987_weight 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_dragonecommercereservezyx987_weight order by createtimeutc DESC,id LIMIT ? OFFSET ?";
	   PREPARE stmt2 FROM @sqlv;
	   EXECUTE stmt2 USING @amountperpage, @myoffset;
       DEALLOCATE PREPARE stmt2;
    end if;
END  $$
DELIMITER ;
--  -------------------------------------------
