Radius Query with vender radius restrictions - mysql

I have the following radius query below. The first select will get me every record within 4 miles. (just to keep things simple, the zip in this example is just a primary key and should not be thought of as an actual zip. I'm just trying to model this as a vendor within a given long/lat.)
5 records are returned within 4 miles of latitude 43 and longitude- -74.37
What I'm looking to do is once the records are found, then do a reverse select on the records found using the restriction column as my new radius restriction to eliminate records where vendors do not want to be found outside a particular distance from their physical location.
In my query example below, I'm expecting to find all results except fonda 12068 because that record although is within 4 miles of the first select only wants to be visible within 1 mile (restriction column) of his physical location. The image however is the actual results produced. Can someone help me to understand what I'm doing wrong?
My thought was I could use the the long/lat along with the restriction from the result of the first query to see if a zip was returned matching the original zip.
SELECT zip1.* FROM zip_detail as zip1
WHERE (3958*3.1415926*sqrt((latitude-43)*(latitude-43)
+ cos(latitude/57.29578)*cos(43/57.29578)
*(longitude- -74.37)*(longitude- -74.37))/180)
<= 4
and zip1.zip in (SELECT zip2.zip FROM zip_detail as zip2
WHERE (3958*3.1415926*sqrt((latitude-zip1.latitude)*(latitude-zip1.latitude)
+ cos(latitude/57.29578)*cos(zip1.latitude/57.29578)
*(longitude- zip1.longitude)*(longitude- zip1.longitude))/180)
<= zip1.restriction)
I created a sqlfiddle example

Related

single (unique) results in sql query result

I'm really stuck:
A little background: I've build a website with a database with multiple tables.
It is about minerals that have fluorescense under different types of UV light (long wave, medium wave and short wave, LW, MW and SW)
The website is working fine, however, with a little bit too much results.
I have a table with all the details of the minierals, I have a table where I put the photo's and with those photo's I save what kind of UV is used.
Now I want a query that filters for specific wave lengts; but I get double results since I've multiple photo's with a specific wave length, see yellow marking in example. 19 results instead of 18 results
How do I get the distinct results? So, in this case, how do I get 18 unique results instead of 19 results as shown?
The query I use right now is
SELECT m.korte_naam, m.url_naam, m.mineraal_id, m.opmerking, m.vindplaats_kort, m.gewicht from `mineraal` m, `foto` f where m.mineraal_id = f.mineraal_id and f.lichtbron = 'SW';
Any help is highly appreciated!
If you only want to select each item once, you can use:
SELECT DISTINCT m.korte_naam, m.url_naam, m.mineraal_id, m.opmerking, m.vindplaats_kort, m.gewicht from `mineraal` m, `foto` f where m.mineraal_id = f.mineraal_id and f.lichtbron = 'SW';
If you want not to keep the same record more than once, you need to check before insertion that record hadn't been inserted already.

Spatialite find the nearest node for the given lat/lon

I am struggle on this for a few days, I am not sure why the query was wrong..
I want to find the nearest point at the given lat/lon.
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))limit 1
It's always return the first row, nomatter what point I use. Then I tried to remove "limit1":
SELECT rid,DISTANCE(geometry, MakePoint(-79.91759, 43.266571))
FROM room2f ORDER BY DISTANCE(Startpoint(geometry), MakePoint(-79.91759, 43.266571))
The result is very strange... The result is sorted by rid rather than distance, I think this is why it always return the first row. Is my query wrong? Or, there is a bug in Spatialite?
Thanks]1
In your sql statement you compare to different distances..
From your geometry in general to your specific point (as I know it is the shortest distance from the point to your geometry).
From the first point of your geometry to your specific point.
Therefore you also get a different ordering of your result!
See the reference to the distance function:
http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html#p13

What is the fromage option in the Indeed XML Job Search API?

Here's the link to the API: https://ads.indeed.com/jobroll/xmlfeed
You might need to login to see it, but here's the raw text. fromage is one of the options and I'm trying to figure out what it does precisely, to no avail:
st Site type. To show only jobs from job boards use "jobsite". For jobs from direct employer websites use "employer".
jt Job type. Allowed values: "fulltime", "parttime", "contract", "internship", "temporary".
start Start results at this result number, beginning with 0. Default is 0.
limit Maximum number of results returned per query. Default is 10
fromage Number of days back to search.
highlight Setting this value to 1 will bold terms in the snippet that are also present in q. Default is 0.
filter Filter duplicate results. 0 turns off duplicate job filtering. Default is 1.
latlong If latlong=1, returns latitude and longitude information for each job result. Default is 0.
co Search within country specified. Default is us See below for a complete list of supported countries.
Nevermind I figured it out by just tweaking in query in the url. It acts as the "days ago posted". For example, if you need to get jobs that were posted in the last 24 hours, you use 1. If you need it within this week, you use 7. It's useful to keep the query fresh and up to date.

Problem with incorrect polygon - Postgis query doesn't work

Well, I have 2 tables, one represents polygons and the second points. I want to know 2 things, the first is the area of the polygons of the first table, the second is the area of the intersection between 2 geometries:
1) the area of the polygons of the first table
2) the area of the union of a buffer of 150 meters radius around my points that is situated inside the polygons.
I made the query to do it, but something went wrong because if I set the WHERE clause to select just one of the polygons of the first table then all works fine, but if I want the data of all polygons, then I get the error: "Error performing intersection: TopologyException: Input geom 0 is invalid: Self-intersection at or near point 12.20304662098798 46.117661703823636 at 12.20304662098798 46.117661703823636"
The first query that works is :
SELECT residenziali.code,
ST_Area(residenziali.the_geom::geography, true) as area_zona,
ST_Area(ST_Intersection(residenziali.the_geom,
(ST_Union(ST_Buffer(fermate.the_geom::geography, 150)::geometry)))::geography,true) as intersect
FROM residenziali
JOIN fermate ON st_contains(residenziali.the_geom , fermate.the_geom)
WHERE residenziali.id = '969'
GROUP BY residenziali.code,residenziali.the_geom
when I remove the WHERE clause it doesn't work anymore, what is the error??
the geometry column of both tables is "the_geom", the first table is "residenziali" and the second is "fermate", both using WGS84. Thanks!!!

Sorting results on a mysql table along longitude and then latitude, NOT a circular radius

Hey all...
I've been tasked with a global search project.
They want to search a database of pictures, all with long/lat
references storied per image in a mysql DB.
However, their search criteria is a little specific.
A simplified table structure is:
id INT auto inc primary
imagePath varchar 255
long DECIMAL 12,7
lat DECIMAL 12,7
I need to provide the SQL statement with a start long/lat position.
They then want to receive 34 images, going south from their initial
long reference.. once they get to -90 (the bottom of the earth) to pop
over 1 degree to the right/East and search up to the north to 90 and
repeat until 34 images are found.
What's confusing me is how to calculate the minus to plus changes..
ensuring that we loop up and down along the earths 'long' reference.
Has anyone done this before..?
I'd LOVE to do it in a single query...
(I've seen how one can fetch results based on a circle radius, but my client.. bless em.. doesn't want to do that)
Ok, back to google!!
Thoughts...
Tris...
Inputs are $the_lat and $the_long
SELECT *
FROM table
WHERE long > $the_long
OR ( long = $the_long AND lat >= $the_lat)
ORDER BY long ASC, lat ASC
LIMIT 34
As is, it's a uni-directional search -- it won't wrap around to longs < $the_long.