Use of HAVING SQL keyword

Peter Lane Posted in Component Development 1 year ago

Are there any existing solutions that use this SQL Keyword?

Replies
gb Peter Lane Replied 1 year ago

I am a humble supplicant...
I am not looking to promote myself, just looking to support the community and moving to improve the use of mapping.

German Michael Zülsdorff Replied 1 year ago

Okay, so you're a rocket scientist!?

Sorry, but I couldn't prevent your post reminding me spontaneously on Shania Twain's That don't impress me much 😂

gb Peter Lane Replied 1 year ago

Thank you for your quick response!

I did not install the code I altered. I created it to respond to the question asked by Michael.

I appreciate your advice.

Indonesian Arsalan Shah Replied 1 year ago

Please don't edit core files that is why you were getting those errors on your installation. Simply use a Plain SQL and no need to edit OSSN files please it takes long time to find out the changes caused issue.

$database = new OssnDatabase();
$database->statement("Your plain query goes here");
$database->execute();
$result = $database->fetch(true);
var_dump($result);
gb Peter Lane Replied 1 year ago

Certainly. I code missing in ossn/classes/OssnDatabase.php to handle that keyword. I attach my suggested fix from line 269. This would allow me to create an SQL query from code like this (which you may recognise as Haversine formula for MySQL):

       sql = "SELECT t.upl_item, t.upl_dodl AS dodl, t.upl_lastupdate AS lastupdate, CONCAT(t.upl_width, '|', t.upl_height) AS itsize, t.upl_ext, itm_id, itm_title, itm_desc, CONCAT(t.upl_width, '|', t.upl_height) AS imgsize, CONCAT(t.upl_path, t.upl_name, t.upl_ext) AS img"
        if(dist > 0 && lat > 0 && lng > 0)
          sql += ", 3956*2*ASIN(SQRT(POWER(SIN(("+lat+"-"+itm_lat+")*pi()/180/2),2)+COS("+lat+"*pi()/180)*COS("+itm_lat+"*pi()/180)*POWER(SIN(("+lng+"-"+itm_lng+")*pi()/180/2),2))) AS distance"

        sql += " FROM mgr_item_search, "+table+" t "
        if(feedtable.length > 0) sql += feedtable

        sql += " WHERE t.upl_item = itm_id AND" 

        if(feedtable.length == 0) sql += " t.upl_user_id = "+mem_id+" AND"
        sql += " t.upl_folder = "+fld+" AND t.upl_del = 0"

        if(kws.length > 0)
            sql += " AND MATCH(itm_title,itm_desc) (AGAINST ? IN BOOLEAN MODE)"
       if(loc > 0) sql += " AND itm_location = "+loc
        if(dist > 0 && lat > 0 && lng > 0)
          sql += " AND itm_lng BETWEEN "+long1+" AND "+long2+" AND itm_lat BETWEEN "+lat1+" AND "+lat2+" HAVING distance <"+ dist

enter image description here

German Michael Zülsdorff Replied 1 year ago

Can you please explain WHAT is showing you that upgrade of the select function is necessary for you, Peter?
And what code you'd like to add in order to achieve what?

gb Peter Lane Replied 1 year ago

I think that shows me that an upgrade to the public function select in classes/OssnDatabase.php is necessary for me.

With all due respect, I find the easiest way to illustrate the syntax is to point you at this page.
https://www.mysqltutorial.org/mysql-having.aspx
or
https://www.w3schools.com/mysql/mysql_having.asp

Indonesian Arsalan Shah Replied 1 year ago

Not exactly sure what you mean by it.