can't get subquery inside CONCAT to work - mysql

I have this code:
GROUP_CONCAT(
CONCAT(
DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'),
' by ',
'\n',
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(je.description, '<br />', '\n'),
''', '\''),
'"', '"'),
'<', '<'),
'>', '>')
),
'\n\n' ORDER BY je.date_entered DESC SEPARATOR ''
) AS enteries
It works fine. When I add this subquery:
' by ', (SELECT first_name FROM users WHERE id = je.created_by),
resulting in this Group Concat:
GROUP_CONCAT(
CONCAT(
DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'),
' by ',
(SELECT first_name FROM users WHERE id = je.created_by),
'\n',
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(je.description, '<br />', '\n'),
''', '\''),
'"', '"'),
'<', '<'),
'>', '>')
),
'\n\n' ORDER BY je.date_entered DESC SEPARATOR '')
AS enteries
it breaks the whole dang thing, causing no errors, but just returning null.
This may look hairy and tangled but I don't have the option to put everything together in php -- it has to all be done in mysql during the selection process.
p.s. I didn't post the enter query because it is ridiculously long BUT I can if you want me to.

When you use a subquery inside group_concat and concat, they will return:
NULL if the subquery returns 0 rows OR a NULL value
something (presumably good) if the subquery returns 1 row
an error (1242) if the subquery returns more than one row
Presumably your subquery didn't return any rows or returned a NULL value.

Here is my final, working solution:
GROUP_CONCAT(CONCAT(DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'), ' by ', IF((SELECT first_name FROM users WHERE id LIKE je.created_by) IS NULL, '', (SELECT first_name FROM users WHERE id LIKE je.created_by)), ' ', IF((SELECT last_name FROM users WHERE id LIKE je.created_by) IS NULL, '', (SELECT last_name FROM users WHERE id LIKE je.created_by)), '\n', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(je.description, '<br />', '\n'), ''', '\''), '"', '"'), '<', '<'), '>', '>')), '\n\n' ORDER BY je.date_entered DESC SEPARATOR '') AS enteries

Related

SQL ignore parenthesis in name field for ORDER BY

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;

How to count the occurrence of specific words in a Mysql db row?

I'm quite new to MySQL. I require to count the number of occurrences of specific words in a MySQL table/columns.
I came across the following code through another post #Raging Bull, which only provides the count of one specific word.
SELECT name,(CHAR_LENGTH(description)-
CHAR_LENGTH(REPLACE(description,' is ','')))/CHAR_LENGTH(' is ') AS
TotalCount
FROM TableName
GROUP BY name
Could someone help me to align it for multiple words to be counted. For example, I want to count "is", "as", "was", "how".
MySQL is not designed for this sort of thing. However, you can just add the values together. I thing the correct method is like this;
select ( (length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' is ', '')) / length(' is ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' as ', '')) / length(' as ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' was ', '')) / length(' was ') +
(length(concat(' ', description, ' ')) - length(replace(concat(' ', description, ' '), ' how ', '')) / length(' how ')
)
from t ;
Note the use of spaces at the beginning and end to capture words at the beginning and end of the description. Also, this assumes that only spaces are used for separating words.
Try using LENGTH instead of CHAR_LENGTH and truncate spaces from String, e.g.:
SELECT name,
ROUND (
(
LENGTH(description)
- LENGTH(REPLACE(description, "is", ""))
) / LENGTH("is")
) AS count
FROM TableName
update
To count multiple words, you can write simillar ROUND queries and add them together, e.g.:
SELECT name,
SELECT(
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) +
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "This", "")) ) / LENGTH("This")) +
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "That", "")) ) / LENGTH("That"))
) AS `count`
FROM TableName
update 2
Here's the query to get the counts as individual columns:
SELECT name,
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is")) AS 'is count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this")) AS 'this count',
ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by")) AS 'by count'
FROM TableName
update 3
Below is the query to get aggregated counts for words:
SELECT 'is', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "is", "")) ) / LENGTH("is"))) AS `count` FROM TableName
UNION
SELECT 'this', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "this", "")) ) / LENGTH("this"))) AS `count` FROM TableName
UNION
SELECT 'by', SUM(ROUND((LENGTH(description)- LENGTH(REPLACE(description, "by", "")) ) / LENGTH("by"))) AS `count` FROM TableName
Here's the SQL Fiddle.
select count(adm_no) from class_manager
Try that where adm_no is the column name and class_manager is the table

mySQL Replace does not replace second token

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.

MySQL split single column

I thought this would be easy... maybe not. I have a table with 'fullname' and I want to split the first name and last name into 2 columns (fname and lname).
The following syntax gives me the data I want:
SELECT
`fullname` ,
SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1) AS fname,
SUBSTRING_INDEX(`fullname` ,' ', 1)AS lname
FROM MyTable
... but how do I then take the 'fname' and 'lname' fields and save them to separate columns in the same table?
[Example data -- If the persons name is John Michael Jones, the 'fullname' field looks like this: JONES JOHN MICHAEL ]
You can just update your table and set the columns to the substrings of the fullname
update MyTable
set fname = SUBSTRING_INDEX(SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
If you want to perform the update on the same table, you can use the following:
update mytable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
See SQL Fiddle with Demo
you want to update into first name ans last name value then you need to used update :
update MyTable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
and if you want to set in upper case then user upper function as well as
update MyTable
set fname = upper(SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1)),
lname = upper(SUBSTRING_INDEX(`fullname` ,' ', 1))

How can I get these four columns to be written concatenated into another column?

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 ...