Mysql Subquery as input second query - mysql

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;

Related

The order of execution for the 'HAVING' clause, without 'GROUP BY' in MySQL

I understand that since the WHERE clause is executed before the SELECT, the following query in MySQL 5 returns an Unknown column error:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
CONCAT(first_name, ' ', LEFT (middle_name, 1), '. ', last_name)
ELSE CONCAT(first_name, ' ', last_name)
END AS name
FROM people_table
WHERE LENGTH(name) < 11;
But using the HAVING clause works:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
CONCAT(first_name, ' ', LEFT (middle_name, 1), '. ', last_name)
ELSE CONCAT(first_name, ' ', last_name)
END AS name
FROM people_table
HAVING LENGTH(name) < 11;
So is the HAVING executed after SELECT, when there is no GROUP BY?
Also I tried in PostgresSQL. Then it looks like that HAVING without GROUP BY here is not allowed. But PostgresSQL does not care about the order of WHERE so the following query works:
SELECT id,
CASE
WHEN LENGTH(TRIM(middle_name)) > 0 THEN
first_name || ' '|| LEFT(middle_name, 1) || '. '|| last_name
ELSE first_name || ' '|| last_name
END AS name
FROM people_table
WHERE LENGTH(name) < 11;
According to the documentation for the SELECT statement, the ability to refer to the SELECT list from HAVING is supported as a non-standard extension to the SQL standard. This shows that HAVING is executed after some parts of the SELECT list by design.
The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.
However, window functions can be included in the SELECT list and these are documented as being executed after HAVING.
Window functions are permitted only in the select list and ORDER BY clause. Query result rows are determined from the FROM clause, after WHERE, GROUP BY, and HAVING processing, and windowing execution occurs before ORDER BY, LIMIT, and SELECT DISTINCT.
The answer is that HAVING can be executed after some parts of the SELECT list and before other parts of the SELECT list.

mysql query - how to merge tables in another table

I have a problem joining tables in the result column. i have a working query which combine different tables using UNION but when i'm extending another table i got an error saying 'The used SELECT statements have a different number of columns'
this is my query:
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(EmDesignation, ', ', Department) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblemployee as e, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
e.RFIDNo LIKE '6424823943')
UNION
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(Course, ', ', Year) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblstudents as s, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
s.RFIDNo LIKE '6424823943')
I have problem with this. Continuation query above
UNION
(SELECT
Barrier
FROM
tblinformation as inf
WHERE
inf.RFIDNo IN (6424823943)
ORDER BY
AttendanceNo DESC LIMIT 1)
The error message is correct. Add NULLs to your second query to match the column number, and it will work.
For example:
SELECT
Barrier,
NULL,
NULL,
NULL,
NULL
FROM
tblinformation as inf
...
The error message states what the problem is. Just improve number of columns in SELECT and it will work correctly.

Mysql - concat string with another sql result

Since I am new to mysql .
select concat ((select to_char(count(*)) from users) ,' of users') from dual;
(or)
select concat ((select to_char(count(*)) from users) ,' of users') ;
I am expecting "100 of users" as result set output. in oracle, the above-mentioned statement will work , but not in mysql. i am getting result value as 1.
You can use the following, using CONCAT (or CONCAT_WS) and a CAST to char:
-- using CONCAT
SELECT CONCAT(CAST(COUNT(*) AS CHAR), ' of users') FROM users;
-- using CONCAT_WS
SELECT CONCAT_WS(' ', CAST(COUNT(*) AS CHAR), 'of users') FROM users;
Instead of TO_CHAR you need to use CAST(... AS CHAR(5)). Since MySQL 8.0 the CONCAT can concat the result of COUNT(*) and of users without a CAST or CONVERT (explicit conversion).
demo: https://www.db-fiddle.com/f/3mVqPxrDZHURyMbX6px4L/0
try below way it will works below is an example not directly yours
SELECT CONCAT(CAST(COUNT(*) AS CHAR(5)), ' of users') FROM Vendor;
http://sqlfiddle.com/#!9/4704eb/18

Presto equivalent of MySQL group_concat

I'm new to Presto and looking to get the same functionality as the group_concat function in MySQL. Are the following two equivalent? If not, any suggestions for how I can recreate the group_concat functionality in Presto?
MySQL:
select
a,
group_concat(b separator ',')
from table
group by a
Presto:
select
a,
array_join(array_agg(b), ',')
from table
group by a
(Found this as a suggested Presto workaround here when searching group_concat functionality.)
Try using this in place of group_concat in Presto ::
select
a,
array_join(array_agg(b), ',')
from table
group by a
Also, if you're looking for unique values only – an equivalent to group_concat(distinct ... separator ', ') – try this:
array_join(array_distinct(array_agg(...)), ', ')
There's no function as of this answer, though the feature has been requested.
The closest equivalent is mentioned in your question.
WITH tmp AS (
SELECT 'hey' AS str1
UNION ALL
SELECT ' there'
)
SELECT array_join(array_agg(str1), ',', '') AS joined
FROM tmp

MySQL: select all the data but if contains ' * ', then show the last four characters

Something like:
SELECT IF( [call_history.callerid contains '*'], [if it contains, keep last four char], [if not, return ' '(not null)] ) AS 'test'
FROM call_history
ORDER BY start DESC;
I recommend reading the manual page on string functions built into MySQL's flavor of SQL: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html
For example:
SELECT IF(LOCATE(callerid, '*'), SUBSTRING(callerid, -4), '') AS 'test'
FROM call_history
ORDER BY start DESC;