Is it possible to do something like:
GROUP_CONCAT(user, price SEPARATOR ', ') AS items
The result is John3.99, Mike24.99
What I need is something like:
John - 3.99, Mike - 24.99
Basically use another type of separator for price field.
GROUP_CONCAT(CONCAT(user, ' - ', price) SEPARATOR ', ') AS items
Or just
GROUP_CONCAT(user, ' - ', price SEPARATOR ', ') AS items
Try this way
GROUP_CONCAT(
DISTINCT CONCAT(user,',',Price SEPERATOR)
ORDER BY items
SEPARATOR ';'
)
Related
I would like rows returned in a MySQL query to be sorted alphabetically by surname for which I have an SQL query like:
SELECT
id,
substring_index(name, ' ', -1) as surname
FROM
my_table
ORDER BY
surname asc
However, some names have parenthesis to denote some special circumstance such as: Laura Angel (retired)
How can I modify my SQL above to ignore the parenthesised text, to sort by surname alphabetically?
Try with nested replaces to remove the parentheses.
SELECT
id,
substring_index(name, ' ', -1) as surname
ORDER BY
REPLACE( REPLACE( surname , '(' , '') , ')' , '') ASC;
Test and modify according to you version of SQL.
Not tested.
You can use this solution:
SELECT id,
substring_index(rtrim(substring_index(name, '(', 1)), ' ', -1) as surname
FROM test.test
ORDER BY
surname asc;
i have the next sub-query:
SELECT SUBSTRING(
(SELECT GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|')
FROM darwin.vt_partidas PT
WHERE PT.Pedimento = P.ID)
,1,30) AS 'Resultado'
FROM darwin.vt_pedimentos P WHERE P.ID=130
I need to concat all results separated with | until i reach 130 characters, but my problem is that if at the end a result doesn't fit example:
i get the first 30 characters but the last result doesn't fit, i get:
result1|result2|result3|result
and i want this:
result1|result2|result3
(if the result doesn't fit, remove all characters from that result)
Thank you guys
Try this
Updated your GROUP_CONCAT and added another step to remove irrelevant data exceeding the 30 max length
SELECT
#str:= left(GROUP_CONCAT( DISTINCT PT.Factura SEPARATOR '|'), 30)
FROM
vt_pedimentos P
INNER JOIN
vt_partidas PT
ON PT.Pedimento = P.ID
WHERE
P.ID = 130;
-- to check whether the last or truncated text exists in the table otherwise remove
select
#str:= left(#str,
(
length(#str) - length(reverse(left(reverse(#str), locate('|', reverse(#str)) - 1)))
)
- 1)
FROM
vt_pedimentos P
where
NOT EXISTS
(
select
1
from
vt_partidas PT
where
PT.Factura = reverse(left(reverse(#str), locate('|', reverse(#str)) - 1))
)
and P.ID = 130;
further enhancement - have it to one sql statement
String manipulation is not a forte of SQL expressions.
But something like this should do it:
SELECT
IF(CHAR_LENGTH( GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|') ) < 130
, GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|')
, SUBSTRING_INDEX(
SUBSTR( GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|') ,1,130)
, '|'
, CHAR_LENGTH( SUBSTR( GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|') ,1,130) )
-CHAR_LENGTH(REPLACE(SUBSTR( GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|') ,1,130),'|',''))
)
)
That's fairly complicated. It will be easier to decipher if we replace the GROUP_CONCAT expression with a placeholder. Let's have res represent GROUP_CONCAT(DISTINCT PT.Factura SEPARATOR '|') expression.
SELECT
IF(CHAR_LENGTH( res ) < 130
, res
, SUBSTRING_INDEX(
SUBSTR( res ,1,130)
, '|'
, CHAR_LENGTH( SUBSTR( res ,1,130) )
-CHAR_LENGTH(REPLACE(SUBSTR( res ,1,130),'|',''))
)
)
Still ugly, but better. Let's break that down.
If the number of characters in res is less than 130, we're done. Just return res.
Otherwise, we need to trim res to 130 characters, we can use SUBSTRING function to do that.
Now, we want to trim the last | and the following characters. To do that, we can get a count of the number | separator characters. Then we know which one the last one is.
(We can get a count of the separator characters by replacing all separator characters with an empty string, then getting the length of that string, and subtracting that from the length of the original string. The difference is the total length of the removed separator characters.
Then we can use that difference in a SUBSTRING_INDEX function to return all of the the characters before the last separator.
It's not a pretty solution. But it does implement an algorithm that satisfies the specification.
I have this 3 columns tab
PRODUCT....CATEGORY......CLASS
X............1.............II
Y............1.............II
Z............1.............II
Y............2.............II
And I want to return a single row with all DISTINCT values of each column:
RESULT
X Y Z 1 2 II
What's the best way to get this result?
Assuming there are no duplicates between the columns, you can use group_concat():
select concat_ws(' ',
group_concat(distinct product separator ' '),
group_concat(distinct category separator ' '),
group_concat(distinct class separator ' ')
)
from tab t;
You can first collect all values in a subselect and then show these values with group_concat
select group_concat(v separator ' ')
from (select product as v from testa
union
select category as v from testa
union
select class as v from testa) t
SQLFiddle
I have the Following Query:
SELECT BUSINESS_NAME, 'KEYWORD', REPLACE(BUSINESS_NAME, ' ', '-')
FROM clearindia.business b
LEFT OUTER JOIN `clearindia`.`keywords_master` km ON km.KEYWORD_TEXT = b.BUSINESS_NAME
WHERE km.KEYWORD_TEXT IS NULL
AND b.business_name='Dey Radio Service'
GROUP BY BUSINESS_NAME
It gives me the following results:
# BUSINESS_NAME, KEYWORD, REPLACE(BUSINESS_NAME, ' ', '-')
'Dey Radio service', 'KEYWORD', 'Dey-Radio service'
REPLACE(BUSINESS_NAME, ' ', '-') is not working correctly and does not replace the second space with a '-'. Why is that?
Please Note: BUSINESS_NAME has a collation of utf_unicode_ci.
How can I get these four columns to be written concatenated into another column?
SELECT CONCAT( calle, ' ', num, ', ', colonia, ', ', cd ) AS geoco
FROM TABLE_1
ORDER BY id
UPDATE table_1
SET geoco = CONCAT( calle, ' ', num, ', ', colonia, ', ', cd )
UPDATE TABLE_1
set col_to_update = CONCAT( calle, ' ', num, ', ', colonia, ', ', cd )
where ...