How to select coordinate from mysql database - mysql

I can select column of type 'POINT' using
select AsText(column) from mytable;
It returns:
'POINT(1.2, 3.4)'
But can I select it as
'1.2, 3.4'
?
This can be done in backend (of course) but can mysql convert it for me?

There are special functions like ST_X, ST_Y, e.g.:
SELECT
CONCAT(ST_X(point_column), ', ', ST_Y(point_column))
FROM
points_table

Related

select data mysql

i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like

Mysql Subquery as input second query

I want to calculate distance from mysql record, firstly I get all cordinates and saved as LineString object, but I have error. What is wrong with my sql?
WITH tmp AS
(SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') FROM track WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate)
SELECT ST_Length(ST_GeomFromText(tmp));
Firstly, MySQL doesn't support the WITH clause; secondly, you should define variables like 10.4 User-Defined Variables.
You can change you sql to this;)
SELECT GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ') INTO #tmp
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;
SELECT ST_Length(ST_GeomFromText(#tmp));
Or just with one query:
SELECT ST_Length(ST_GeomFromText(GROUP_CONCAT(CONCAT_WS(' ',lat,lng) SEPARATOR ', ')))
FROM track
WHERE vh_id='75' AND DATE(tdate)='2016-06-09' ORDER BY tdate;

Find table names having a particular value in 'any columns'

I was looking for this Stackoverflow Question to find my solution. But this one deals with when the column name is known. But in my case the column name is unknown. i.e. it can be col1, col2, or colN.
Right now I only have the table names from my database using this query:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='db_name';
Now with another query(joining together) I want to scan the tables for all the columns that match the given value. Is it possible with MySql using any built-in function? Or Can any sql tweaking do this trick?
If its one time activity, why cant you run a query to generate the query that give you result.
I dont know a better solution :)
Queries will be something like:
SET group_concat_max_len = 100000;
SELECT GROUP_CONCAT(
CONCAT( 'select "',TABLE_NAME,'.',COLUMN_NAME, '" from `',TABLE_NAME,'` where `', COLUMN_NAME, '` in ("val0", "val1") limit 1')
SEPARATOR ' UNION ALL ' ) as query
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='db_name' INTO OUTFILE '/tmp/queries.sql' \G
Now you can run source the file.
SOURCE /tmp/queries.sql;

Finding language in string (doing substring?) with MySQL

I have a table with languages which s_name value looks like this:
'en_UK'
'en_US'
'de_CH'
'de_AT'
I want to get all the distinct languages, without the country part. So for example, in case I just had those of the example, I would need to get:
en
de
What would be the best way of doing so?
I have this right now:
SELECT DISTINCT SUBSTRING(name,1,2)
FROM my_languages
Try this:
SELECT DISTINCT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
OR
SELECT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
GROUP BY langName
Check this link MySQL STRING Functions
Here is a simple way:
select distinct left(s_name, 2)
from t
This assumes the language name is the left two characters.
This will work in Oracle as well as in MySql I think:
SELECT SUBSTR('en_UK',INSTR('en_UK','_')+1) country FROM dual
/

MySQL select based on regular expression

In MySQL, I need to select all rows where a field is not an IP address (e.g. 12.32.243.43). Can this be done with MySQL only?
For example: I tried the following but it doesn't match.
select * from mytable where field not like '%.%.%.%'
Sure can. You would be looking for something like:
SELECT * FROM `table` WHERE NOT( some_field REGEXP '^[0-9+]\.[0-9]+\.[0-9+]\.[0-9+]')
If using regular expression is not a requirement, this shall deliver the solution:
select stringBasedIp from x
where inet_aton(stringBasedIp) is null;
select stringBasedIp, (inet_aton(stringBasedIp) is null) as isInvalidIp
from x;
Sample data:
create table x(stringBasedIp varchar(16));
insert into x values
('255.255.255.255'),
('0.0.0.0'),
('0.0.0.300'),
('0.0.0.-1'),
('0.0.0.A'),
('192.168.0.1'),
('400.168.0.1'),
('12.32.243.43'),
('12.32.243.430');
Here are the list of invalid ip:
STRINGBASEDIP
0.0.0.300
0.0.0.-1
0.0.0.A
400.168.0.1
12.32.243.430
Live test: http://www.sqlfiddle.com/#!2/2a4ec/1