MySQL snafoo with GROUP_CONCAT - mysql

This works:
SELECT DISTINCT HEX(group_uuid) AS hexid
FROM schema.table
>9B3D8DE01E1E049DA9F17D42B324AA66
>CDF112D740CE14EA08CA90E3C937DE8D
as does this
SELECT GROUP_CONCAT('\'', HEX(group_uuid), '\' ') AS hexid
FROM schema.table
>'9B3D8DE01E1E049DA9F17D42B324AA66','9B3D8DE01E1E049DA9F17D42B324AA66' [etc...]
and this
SELECT GROUP_CONCAT(DISTINCT HEX(group_uuid) SEPARATOR '\' ') AS hexid
FROM schema.table
>9B3D8DE01E1E049DA9F17D42B324AA66' CDF112D740CE14EA08CA90E3C937DE8D
this, however,
SELECT GROUP_CONCAT('\'', DISTINCT HEX(group_uuid), '\' ') AS hexid
FROM schema.table
Error Code: 1064. You have an error in your SQL syntax; INSERT COIN
does not.
Is there some kind 'PREFIX' keyword that can be substituted or another syntax I'm supposed to follow?

Are you looking for
GROUP_CONCAT(DISTINCT CONCAT('\'',HEX(group_uuid),'\'') SEPARATOR ' ')

Related

select table and replace column fields by value from another table in mysql

I have been working in ms SQL from the last 3 year, I am new in MySQL, recently we have some MySQL requirement to select a table and replace column fields by value from another table.
I have converted working ms SQL query to MySQL. while executing MySQL query getting an exception. attached table description.here enter image description here
Please advise me on this.
Following are my tried queries
SET #v_Type := 'Account'; // where condition type =account
SET #select := 'SELECT';
SET #columns:= ( SELECT CONCAT('CONVERT(' , `ColDataType` , ', Col' , CONVERT(VARCHAR, `ColNum`) , ') AS [' , `ColName` , '],') FROM `Names` WHERE `TYPE` = #v_Type FOR XML PATH('') )
SET #where:='FROM `Data` WHERE [Type] = ''' + #v_Type + ''''
SET #statement = CONCAT(#select,#columns,#where);
PREPARE myStatement FROM #statement;
EXECUTE myStatement;
While executing I got the error like
Query: set #columns:= ( SELECT CONCAT('CONVERT(' , ColDataType , ', Col' , CONVERT(VARCHAR, ColNum) , ') AS [' , ColName , '],') ...
Error Code: 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 'VARCHAR, ColNum) , ') AS [' , ColName , '],') FROM `Name' at line 2
Thanks in advance.
SELECT CONCAT( 'SELECT ', GROUP_CONCAT(CONCAT('col', colnum, ' ', colname)),
' FROM data',
' WHERE type = ?')
INTO #sql
FROM names
WHERE type = #v_type
GROUP BY type;
PREPARE myStatement FROM #sql;
EXECUTE myStatement USING #v_type;
DROP PREPARE myStatement;
fiddle

How to concatenate strings in a Select statement in MySQL

I have to run a query in SQL Server using data from a MySQL database. When I needed to do the opposite, I found an easy way to accomplish what I needed writing an update query using the select statement in SQL Server.
In SQL Server I wrote:
SELECT 'update sgidb.Example set MySQLCol1 = ' + cast(MSSQLCol1 as varchar(max)) + ' where MySQLCol2 = ' + cast(MSSQLCol2 as varchar(max)) + ';' FROM MSSQLTable
That resulted in a bunch of update statements with the keys I needed like:
'update sgidb.Example set MySQLCol1 = 12 where MySQLCol2 = 45;
But when I tried to do the same in MySQL I got a bunch of syntax errors. The web told me MySQL don't need the + operator to concatenate strings in a sentence, but it didn't work, neither writing the concatenate function explicitly. Any ideas?
You can use the CONCAT function which is available in MySQL as well as in SQL, like this:
SELECT CONCAT('update sgidb.Example set MySQLCol1 = ' , MSSQLCol1 , ' where MySQLCol2 = ' , MSSQLCol2 , ';' )FROM MSSQLTable
Now in the above solution you need to take care of the blank space after or before or even after and before the statement.
For tackling the above situation what you can do is to use the function CONCAT_WS, which is available in MySQL as well as in SQL:
SELECT CONCAT_WS(' ', 'update sgidb.Example set MySQLCol1 =' , MSSQLCol1 , 'where MySQLCol2 =' , MSSQLCol2 , ';' )FROM MSSQLTable
CONCAT_WS function adds two or more strings together with a separator.
Now no need to take care of the spaces that you need to put to avoid the syntax error anymore.
Please note that, CONCAT_WS is going to handle null as well. But in case of CONCAT, if any of the variable/field is null then the entire CONCATENATED result becomes null.
Here's how you can do it
SELECT concat("update sgidb.Example set MySQLCol1 = ",MSSQLCol1,"where MySQLCol2 = ",MSSQLCol2,";") FROM MSSQLTable;
Both SQL Server and mysql have got CONCAT function.
You can use the below query in both RDBMS.
SELECT CONCAT('update sgidb.Example set MySQLCol1 = ' , MSSQLCol1 , ' where MySQLCol2 = ' , MSSQLCol2 , ';' )FROM MSSQLTable
You may try concat to concatenate your string.
Example as:
SELECT CONCAT('MySQL CAST example #',CAST(2 AS CHAR));
#ouput
MySQL CAST example #2
In your above update query your set and where column may have varchar, but if you closely look into your query ' is missing in both of the column names. Please try the below query.
SELECT CONCAT('update sgidb.Example set MySQLCol1 = ''',
CAST(MSSQLCol1 AS VARCHAR(MAX)),
''' where MySQLCol2 = ''',
CAST(MSSQLCol2 AS VARCHAR(MAX)) ,
''';' )
FROM MSSQLTable;

MySQL 1064 syntax error in stored procedure

I have stored procedure in local mysql version 5.7.18-log which has this block
but it is not working on AWS RDS 5.7.23-log.
SELECT GROUP_CONCAT( CONCAT(' GROUP_CONCAT(IF(slf.Name = ''', t.NAME, ''' , slfv.Text, NULL)) AS ', t.NAME )) INTO #PivotQuery
FROM (SELECT slf.NAME FROM searchlogfields slf GROUP BY slf.NAME) t;
It throws
syntax error on "SELECT GROUP_CONCAT( CONCAT(' GROUP_CONCAT(IF(slf.Name = ''', t.NAME, ''' , s"
When tried to run this part of the stored procedure in the new query without Begin&End it works.

MySQL group_concat and concat

I have these data on MySQL table cart.
I would like to run a query that would output like this:
Advance Technique Conditioner (2), Advance Technique Gel (1)
So I was doing a group_concat query and was trying queries to output my desired output. This is the query that outputted the nearest to the desired:
select concat(group_concat(product separator ', '), group_concat(quantity separator ', ')) as 'prod' from cart where debt_no='0000001'
But as expected, it outputted like this : Advance Technique Conditioner, Advance Technique Gel2, 1
How can I get the desired output?
CONCAT() should be inside GROUP_CONCAT()
GROUP_CONCAT(CONCAT(product, ' (', CAST(quantity AS CHAR(15)), ')') SEPARATOR ', ')
remember that the default length of GROUP_CONCAT() is 1024. So if you want to change the limit execute the following line,
SET [GLOBAL | SESSION] group_concat_max_len = val; -- val is the new length
SELECT GROUP_CONCAT(CONCAT(product, ' (', quantity, ')') SEPARATOR ', ')
You need to do the concat() first, then the group_concat():
select group_concat(concat(product, ' (', quantity, ')') separator ', ') as prod
from cart where debt_no='0000001'

INSERT INTO mySQL procedure

INSERT INTO in MySQL>
this is a part of code a procedure:
if ParentList is null then
set #sql=concat('insert into ', table_name, tabSave, '(md5Id, CommentsId, Parent, Content, UserId, Title, Nick) ',
'values(', #md5Id, ', ', CommentsId, ', null, \'', Content, '\'' ,UserId,',',Title,',',Nick, ')');
else
set #sql=concat('insert into ', table_name, tabSave, '(md5Id, CommentsId, Parent, Content, UserId, Title, Nick) ',
'values(', #md5Id, ', ', CommentsId, ', ', #Parent, ', \'', Content, '\ ,', UserId,', ', Title,', ', Nick, ')');
end if;
This character sign ' is badly inserted and I dont know where :
My error:
CALL AddDynamicTable3('asd65xb', 'xxx', 5, NULL, 'MojWstep', 22, 'New title', 'Ethan') Error Code: 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 '22,New title,Przemo)' at line 1
XXX - table_name
other variables are not significant example tabSave, table_name ...
Earlier I wrote the same procedure but without three variables UserId, Title, Nick and its OK but when I put this variables I only see ERROR.
See if this works:
'\ ,', UserId,',
from the second query, change to
'\ ', UserId,',