Inserting a substring value into an already existing entry - mysql

Currently I have a database with the following parameters and entry:
Now I want to fill the column status_from in this particular entry with the id 1 with a substring of the action column. Which is basically I want to fill my status_from column with a word that comes after from and before to of the action column. To achieve this I've tried using the following statement:
INSERT INTO crm_logs2(status_from) SELECT status_from WHERE id='1' VALUES (substring_index(substring_index(action, 'from', -1),'to', 1))
But doing this gave me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VALUES (substring_index(substring_index(action, 'from', -1), ...' at line 1

You need to use the UPDATE statement
UPDATE crm_logs2
SET status_from = (SUBSTRING_INDEX(SUBSTRING_INDEX(action, "from", -1), "to", 1))
WHERE id = 1

update crm_logs2
set action=(select SUBSTRING_INDEX(SUBSTRING_INDEX(action, ' ', 6), ' ', -1) from
crm_logs2 where id=1)where id=1
Try this and let us know if it works

Related

MySQL #1241 - Operand should contain 1 column(s)

I want to update the column 'page_inv', so it will replace two last chars of this column, and add new data, for example:
'{"userItems":[{item1}]}' -> '{"userItems":[{item1}' -> '{"userItems":[{item1}, {item2}]}'
UPDATE `users`
SET `page_inv`=CONCAT((SELECT `page_inv`, SUBSTRING( `page_inv`, 1, CHAR_LENGTH( `page_inv` )) -2), ',{newItem}]}')
WHERE `STEAMID`=76561198147
My DB:
Something like this?
UPDATE `users`
SET page_inv= CONCAT(left(page_inv, length(page_inv) - 2), ',{newItem}]}')
WHERE STEAMID = 76561198147;

Use of in clause value to another sql query

I have one variable:
FarmerServiceID+= "'"+tabledata1.get(k)+"',";
My problem is: the values which are coming tabledata1.get(k) ends with ','
and what happens is I use this FarmerServiceID in the following query.
select farmerid from tblfarmer where farmermapid in("+ FarmerServiceID+");
error: syntax error near ')'
I know error is coming because of ','
How do I remove this error?
the error occurs because of your FarmerServiceID has a , (comma) at the end of the string.
I will suggest you to remove the comma at the end of the string before you do the SQL query.
FarmerServiceID += "'"+tabledata1.get(k)+"',";
FarmerServiceID = FarmerServiceID.substring(0, FarmerServiceId.lastIndexOf(',')); -- Remove the comma at the end of the string
Then do your SQL Query
select farmerid from tblfarmer where farmermapid in("+ FarmerServiceID+");

am trying to execute the query using the CONCAT function in the mysql console

the query is as follows
SELECT id, phonenumber CONCAT( 'A reminder for ', name, 'underfive number ', underfiveNO, 'for ', message, 'tomorrow.If shot was administered please ignore this message ' )
FROM appointment WHERE MONTH (current_date) = MONTH (appointmentdate)
AND DAY (current_date) < DAY (appointmentdate)
AND (NOT lastnotified = current_date) OR lastnotified IS NULL;
the error am getting is
ERROR 1064(42000) 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 '('A reminder for',name,'under five number',undefiveNo, 'for', message'at line1
You forgot a comma
SELECT id, phonenumber, CONCAT( '...
^-----------------herre
There should be separator in between phonenumber and CONCAT() in your query: try with this
SELECT id, phonenumber, CONCAT('A reminder for ', name, ' underfive number ', underfiveNO, ' for ', message, ' tomorrow. If shot was administered please ignore this message' )
FROM appointment WHERE MONTH (current_date) = MONTH (appointmentdate)
AND DAY (current_date) < DAY (appointmentdate)
AND ((NOT lastnotified = current_date) OR lastnotified IS NULL);
and also as you are adding a OR condition on lastnotified column, it should be enclosed with in paranthesis to meet either of the condition

MySql: updating a column with the column's content plus something else

I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'

SQL Error 1630: Function SUBSTRING does not exist.. huh?

Right. So I've created a stored procedure in a MySQL DB which happens to use SUBSTRING.
Running the procedure via a query gives me:
SQL Error 1630: Function mydatabase.SUBSTRING does not exist
Beg your pardon?
Is there a space after the method call to Substring before the first parenthesis?
It appears on Line 40:
IF i > 1 AND j > 1 AND (s1_char = SUBSTRING (s2, j - 1, 1))
i.e. Ensure
select substring(CustomerName, 1, 4) AS CustName from MyTable;
instead of:
select substring (CustomerName, 1, 4) AS CustName from MyTable;