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  $$
--  ------------------------------------------------------
