I've decided to detect distance between two points. One of the points is static -35.735019 51.410041, however, the other one is a point which is given by a database field.
Here is my code:
SELECT
r0_.restaurant_point AS restaurant_point_0,
ST_Distance(GeomFromText('POINT(35.735019 51.410041)'), GeomFromText('POINT(r0_.restaurant_point)')) *
100 AS sclr_1
FROM restaurants r0_
We stored r0.restaurant_point as a VARCHAR. Take the string below as an example: 35.73062161548168 51.410930156707764
Although it would be worked if I change r0.restaurant_point with static value, it can't get measure with database field.
Is there anyway for binding this value or another way to resolve?
To concatenate the value of the column restaurant_point in the string that represents the point, you can use CONCAT().
GeomFromText(CONCAT('POINT(', t.restaurant_point, ')'))
In your case you are just trying to create a Point NOT from the coordinates in the column, BUT from the column name itself. you need to change your code as follows:
SELECT
r0_.restaurant_point AS restaurant_point_0,
ST_Distance(
GeomFromText('POINT(35.735019 51.410041)'),
GeomFromText(CONCAT('POINT(', r0_.restaurant_point, ')'))
) * 100 AS sclr_1
FROM restaurants r0_
Related
I'm a little bit stuck.
I have an SQL column that contains weather codes (like Rain, Snow, etc.)
It is comma separated so the column would have a value of something like
rain,snow,haze
Now, I want to select the rows that contain values from an array.
I have an SQL code that is something like this:
SELECT * FROM locations WHERE currentWeather IN ('rain', 'snow', 'cloudy') ORDER BY name ASC
The problem is that this obviously works when currentWeather column only contains one item.
Is there a way to do it so that if the column value contains any of the items from the given array, it selects it?
Also, would it select it twice if two items match?
Best wishes
Use unnest in a subselect.
Select distinct A.myArray from (select unnest(column) as myArray from table) A where A.myArray in (your words to filter for)
Notice that using arrays in sql isn't very ideal and does not follows normalization rules. Your tables should ideally not contain arrays but rather just several rows each one containing the specific value you Want. It prevents issues such as this one.
To avoid the selection of repeated values, use the Distinct keyword right after you write select.
Rsference:
https://www.w3resource.com/PostgreSQL/postgresql_unnest-function.php
WHERE FIND_IN_SET(currentWeather, "rain,snow,cloudy")
Picks apart the string at commas (only) to see if currentWeather is any one of those 3 'words'.
See also FIELD(...)
I need to save numbers to database as it is please see the example.
currently i save number as 22.5 and in database it is saved as 22.
number in database is set as integer
//sometimes $number can be "33.5" or "33" or "1" or "1.003"or "03"
$sql = "INSERT INTO active (name, number) VALUES ('somename','$number')";
If you make your number column as DECIMAL data type, it will hold the decimal point with all numbers. Integer columns cannot hold decimal points.
If it is not your requirement, better use VARCHAR as data type (But NOT RECOMENDED).
Hope this helps.
First of all, you need to check the structure of active table. From your question, I understand that various types of number values is going to be saved to number column. And I guess the type of number column in active table is numeric for int types. For example, INT or BIGINT or any other integer types.
Here is what I'm usually doing. Just use varchar for number field. It might not sound like professional. But it's very easy to use. By doing so, you won't have any problem with saving numeric value to the column. I have got an similar issue before while saving latitude and longitude value to the table. And php has got many functions to get actual float or int value from string. So this is an easy and simple way to fix.
One of the easiest and reliable way to save the numbers/integers/floats to the database is to convert them to string and then storing them into the database.
Note : Your database column type should be of type string/text.
One of the advantage of this method is that you can overcome the limits of int/float/double.
$string_number = (string)$number;
$float_number = (float)$string_number;
If you want to save number as 22.5 and in database. Then you need to change table column structure. Instead of Integer/Int set to float Then Next set length 10,2
for information please check below screen short
Then you get proper output Happy Programming
I want to fetch the total bill amount of particular day, but in my database all the amount are inserted with "£" symbol. Now I want to sum all the column values.can anyone help me to find out
For example:
I have created a table called order and the columns are id, totamt. In totamt column data is inserted like float values eg. £2.00, £90.00 etc...
I have tried to add the values using Sum function in sql query. It adds the value but the total amount is wrong.
I guess, as others in comments have guessed, your totamt column has a VARCHAR() data type, not the FLOAT data type you mentioned in your question.
To solve this problem the best way, you should rework your database so your totamt column has a DECIMAL(12,2) data type. Why? Strings are hard to use for arithmetic operations as you have discovered. Also, floating-point arithmetic has some quirks making it not so good for money values.
In the meantime, you need to add code to your query to edit your totamt strings so they look like numbers before you give them to the SUM() operation.
This may work for you to convert your values. It removes the currency symbols and expresses the amount as a decimal value.
CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2)) totamt
So you can then do
SELECT SUM(CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2))) grand_total,
id
FROM table
GROUP BY id
I have a database with 150,000 records and I need to match its FULL column value or records, with some parts of the string.
**
As i want to check if the STRING contains the COLUMN records and NOT!
if the COLUMN contains the string
**
(Example below)
For testing purposes
Lets say the database has a TABLE , 1 COLUMN and 1 record as the records are similar to this:
come to me
and i need to match it with this #STRING
She wants to come to me now
I want to execute something similar to :(but this doesn't work of course)
SELECT * from TABLE where #STRING like '%'+COLUMN+'%'
I can't seem to solve this with SQL the usage of PHP is possible but prefer if the solution is with SQL but if the solution with PHP is available please propose it and note that the database has over 150,000 records
SELECT * from TABLE where LOCATE(COLUMN, #STRING)>0;
LOCATE(a,b) returns a number giving the character location of a in b, or returns 0.
See Mysql LOCATE()
The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. That probably doesn't affect your use case, though if it became an issue you could CONVERT() the binary strings to a locale and use LOWER() to get lower case.
MySQL String Functions
The dynamic like syntax for mysql is
SELECT * from TABLE where #STRING like CONCAT('%',COLUMN,'%')
I am trying to improve an old system (written ages ago) where every mysql queries are glued from string. So example of that query looks like
"SELECT * FROM User WHERE id > '3'"
Id column is of course bigint and PK.
What does mysql do with '3' in query where id should be a int value? I assume it is treated as a string (due to '') so this value is casted into int during analyze/optimize process by mysql. Am I right?
//UPDATE
I probably asked wrong way. There are two way to handle it.
(Fast) Mysql automatically detects that id should be int and rewrite/cast a query to
SELECT * FROM User WHERE id > 3
before send it to DB engine
(Unbelievable) Mysql does
SELECT * FROM
then in loop apply condition WHERE id > '3' and cast it for EVERY row
I just want to be sure that second option is impossible.
MySQL will always cast the string to a number for comparing, which in this case this is the right thing to do (it will use the index on the column to find the values).
If your column is a string and you compare it to an integer constant MySQL will cast the column to an integer and not use the index.
MySQL will automatically cast it into the correct column type. If it cannot for some reason, it will throw an error.
Make sure to use prepared statements with PDO or MySQLi in any case where the parameter may come from an unsafe source (the user, an external API).