What is the delimiter for json output? - json

I'm querying a db that returns json output using odbc using isql. Here's the help for isql.
isql -h
**********************************************
* unixODBC - isql *
**********************************************
* Syntax *
* *
* isql DSN [UID [PWD]] [options] *
* *
* Options *
* *
* -b batch.(no prompting etc) *
* -dx delimit columns with x *
* -x0xXX delimit columns with XX, where *
* x is in hex, ie 0x09 is tab *
* -w wrap results in an HTML table *
* -c column names on first row. *
* (only used when -d) *
* -mn limit column display width to n *
* -v verbose. *
* -lx set locale to x *
* -q wrap char fields in dquotes *
* -3 Use ODBC 3 calls *
* -n Use new line processing *
* -e Use SQLExecDirect not Prepare *
* -k Use SQLDriverConnect *
* -L Length of col display (def:300) *
* --version version *
* *
* Commands *
* *
* help - list tables *
* help table - list columns in table *
* help help - list all help options *
* *
* Examples *
* *
* isql WebDB MyID MyPWD -w < My.sql *
* *
* Each line in My.sql must contain *
* exactly 1 SQL command except for the *
* last line which must be blank (unless *
* -n option specified). *
* *
**********************************************
I experimented with the -d flag, e.g: no -d flag in a dummy query:
cat bla.sql
select 1 as bla, 2 as ha
isql -v -c -b BQ < bla.sql
+---------------------+---------------------+
| bla | ha |
+---------------------+---------------------+
| 1 | 2 |
+---------------------+---------------------+
SQLRowCount returns -1
1 rows fetched
I can make csv using -d, like so:
isql -v -c -b -d, BQ < bla.sql
bla,ha
1,2
But my real query will contain json structure data. What is the appropriate deliminator to use with the -d flag here?

Related

Filter query column with time() Laravel

I have read a lot of posts in the forum but haven't been able to understand this error.
I need to count user's sessions ( login times?) with the timestamp less than 3 days.
My query.
Session::where('user_id', $user->id)
->whereRaw('`timestamp` < ' . time() -3 * 24 * 60 * 60)
->orWhereRaw('timestamp' . ' IS NULL')
->orderBy('date')->limit($usc - $user->devices)->delete();
Here is my mistake.
ReflectionException
Class App\UseCases\UserService does not exist
If I remove ->whereRaw('`timestamp` < ' . time() -3 * 24 * 60 * 60) from query, the mistake disappears.
I don't seem to find the error.
Thank you in advance.
You can use the whereDate filter provided by laravel:
Session::where('user_id', $user->id)
->whereDate('timestamp', '<', date('Y-m-d', time() -3 * 24 * 60 * 60));
->orWhereRaw('timestamp' . ' IS NULL')
->orderBy('date')->limit($usc - $user->devices)->delete();
Docs: https://laravel.com/docs/7.x/queries#where-clauses

Combine two MySQL SELECT queries from single table

I have an SQL statement like this:
SELECT
(111.045 * DEGREES(ACOS(COS(RADIANS({$lat}))
* COS(RADIANS(lat))
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat}))
* SIN(RADIANS(lat))))) AS distance,
(`f1` + `f2` + `f5` + `f6`) AS sum
FROM `shops`
WHERE distance <= 25
where first part returns shops within certain distance and second part returns sum of given fields in the same row of the same table.
Both parts work separately but do not return results when combined.
Please tell me what i do wrong?
Don't use alias in where
SELECT
(111.045 * DEGREES(ACOS(COS(RADIANS({$lat}))
* COS(RADIANS(lat))
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat}))
* SIN(RADIANS(lat))))) AS distance,
(f1 + f2 + f5 + f6) AS sum
FROM shops
WHERE (111.045 * DEGREES(ACOS(COS(RADIANS({$lat}))
* COS(RADIANS(lat))
* COS(RADIANS({$lon}) - RADIANS(lon)) + SIN(RADIANS({$lat}))
* SIN(RADIANS(lat))))) <= 25

MySQL radius search

I am making an application with radius search, here is the example I learned from
https://developers.google.com/maps/articles/phpsqlsearch_v3#findnearsql
In my case
I do
SELECT (3959 * acos(cos(radians(-37.814107)) * cos(radians(lat)) * cos(radians(lng) - radians(144.96327999999994)) + sin(radians(-37.814107)) * sin(radians(lat)))) AS distance;
when lat = -37.8141070000, lng = 144.9632800000
SELECT (3959 * acos(cos(radians(-37.814107)) * cos(radians(-37.8141070000)) * cos(radians(144.9632800000) - radians(144.96327999999994)) + sin(radians(-37.814107)) * sin(radians(-37.8141070000)))) AS distance;
MySQL output the distance as NULL, why? Did I do something wrong?
How I fix it? Sorry I am really NOT good at math.
Basic SQL query
SELECT * FROM table
You must have a MySql table in your query with lat & lng as fields.
ie
SELECT (3959 * acos(cos(radians(-37.814107))
* cos(radians(lat)) * cos(radians(lng) - radians(144.96327999999994))
+ sin(radians(-37.814107))
* sin(radians(lat)))) AS distance FROM table;

Mysql doesn't recognize mathematical operation

I have the following query to calculate geo distance:
SELECT (
(ACOS(SIN(39 * PI() / 180) *
SIN(`latitude` * PI() / 180) +
COS(39 * PI() / 180) *
COS(`latitude` * PI() / 180) *
COS((32–`poi.longitude`) * PI()/180)
) * 180 / PI()
) * 60 * 1.1515
) AS distance
FROM `poi`
HAVING distance <= 10
ORDER BY distance ASC
When I run the query I have the error:
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '– poi.longitude) * PI()/180...
So if I replace (32–poi.longitude) with a number like (3) then it works, but even if i use (32–21) as a mathematical operation it throws error. I replace poi.longitude with longitude (with quotes), longitude and so on but nothing seems to work. Any ideas?
The reason of the syntax error is because you have wrap the table name and column name with backtick as one causing the server to find for an unknown column.
`poi.longitude` -- searches for column name [poi.longitude] and not
-- [longitude] from table [poi]
it should be
`poi`.`longitude`
or
poi.longitude
You are not using the proper minus sign, -.

Mysql error when trying to get distance from long and lat

basically I have a database that is setup in Longitude and Latitude. I have been trying for hours now to fix an issue I have with MySql and I can not find my error. Here is the error I am getting:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '€“ longitude) * PI() / 180))* 180 / PI()) * 60 * 1.1515)*1.609344) AS distance' at line 1
Here is MySql database query to get the distances from long and lat:
$res = mysql_query("SELECT *, (((ACOS(SIN(".$latitude." * PI() / 180) * SIN(latitude *
PI() / 180) + COS(".$latitude." * PI() / 180) * COS(latitude * PI() / 180) *
COS((".$longitude." – longitude) * PI() / 180))* 180 / PI()) * 60 * 1.1515)*1.609344)
AS distance") or die(mysql_error());
Please please please help!!!
The minus-sign looking character at which the error occurs doesn't appear to be a minus-sign. Perhaps you used Word (or a similar "helpful" editor) to construct the query, and it substituted a "smart" version of a minus-sign, like an en-dash?
I replaced that character with a proper minus sign and the query worked.
I'm not sure why you are doing SELECT *, and you have no FROM clause specified.
Remove the *, from the query or add the FROM and the table you fetch the data from.