MYSQL Update from a select subquery - mysql

The data in the Response column is = blahblahblahTYPE=ERRORblahblah. I then run the query below to just target the 'ERROR' string.
SELECT substr(Response, instr(Response, 'ERROR'), 5)
FROM table
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate();
The query above returns: ERROR.
I basically want to update ERROR to SUCCESS without editing the rest of the data.
Would the query below work? Is there a better way to do this?
UPDATE table
SET Response = 'SUCCESS'
WHERE (
SELECT substr(Response, instr(Response, 'ERROR'), 5)
FROM table
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate()
);
Thanks for your help in advance!

UPDATE table
SET Response = REPLACE(Response, 'ERROR', 'SUCCESS')
WHERE id = 721451
AND Status = 'false'
AND Response LIKE '%<status>Unknown</status>%'
AND date >= curdate();
Refer https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Related

UPDATE from SELECT but still return selected data in MySQL

I have read a bunch of ways that has gotten me this far. But I can't get to the finish line.
I have a table of coupon codes. I want to use one transaction to select the next available code, mark it as used and input the order number. I can get the update and nested select to work, but I cannot figure out how to actually return the coupon code from the select. It just returns 1 row updated.
Here's what I've got:
UPDATE `prcoupon` pr
SET
`pr`.`status` = '1',
`pr`.`invoicenumber` = '09990002'
WHERE
`pr`.`couponCode` = (SELECT
`prcoupon`.`couponcode`
FROM
`prcoupon`
WHERE
`status` = 0
LIMIT 1)
Sample data
What I need returned is: couponCode: SL2T-03A0-JVCY-W2XMXG
If I understand correctly, you can try to use UPDATE ... JOIN with ROW_Nunber windwon function.
UPDATE prcoupon pr
JOIN (
SELECT *,ROW_NUMBER() OVER(ORDER BY couponCode) rn
FROM prcoupon
WHERE status = 0
) t2 ON pr.couponcode = t2.couponcode
SET pr.status = 1,
pr.invoicenumber = '09990002'
WHERE rn = 1
sqlfiddle

getting syntax error while using LIKE in sql query

When I create sql query like this:
param = (date, )
query = SELECT date_added from deploy_requests WHERE date_added LIKE '%s %'
query.execute(query, param)
i get Syntax error
but if I use the not recommended way
query = SELECT date_added from deploy_requests WHERE date_added LIKE '{} %'.format(date)
I figured out, this is how I did it.
def get_deployment_times(date):
date = "{}%".format(date)
param = (date, )
query = "SELECT date_added from deploy_requests WHERE date_added LIKE %s"

Not sure what is the execution stack of mySql fo my below query

So, I am trying to update the status_modified_time only if the status has changed, else keep it the same.
UPDATE table SET status = <new_status>,
status_modified_time = IF(status = <new_status>, status_modified_time, now()) WHERE id = <id>
this query makes status_modified_time = status_modified_time
UPDATE table SET status = <new_status>,
status_modified_time = IF(status = <old_status>, status_modified_time, now()) WHERE id = <id>
this query makes status_modified_time = now()
is it that mysql is updating the status field first and then checking the condition??
Left to right order of evaluation!
SQL UPDATE order of evaluation
I checked my query by updating the modified_time first.

Add a not empty check for a date field into a query

Is there a way to alter this query so that it would check if frp_fundraisingprogram.start_date is not empty or contains the default value?
SELECT frp_fundraisingprogram.id AS id
FROM frp_fundraisingprogram
WHERE frp_fundraisingprogram.start_date <= DATE_ADD(UTC_TIMESTAMP(), INTERVAL - 2 day)
AND frp_fundraisingprogram.date_entered > '2015-04-09 16:55:18'
AND NOT EXISTS
(SELECT * FROM aow_processed
WHERE aow_processed.aow_workflow_id='9bc1bb2e-cd5a-5c75-cc68-5526ae30331e'
AND aow_processed.parent_id=frp_fundraisingprogram.id
AND aow_processed.status = 'Complete' AND aow_processed.deleted = 0)
AND frp_fundraisingprogram.deleted = 0
Just include that in your where clause...
`...WHERE not isnull(frp_fundraisingprogram.start_date)
AND frp_fundraisingprogram.start_date != '0000-00-00 ...`
That is if you are trying to exclude these records. You can reverse the logic if you only want these records to show up.

MYSQL returning no records from URL left string

SELECT
domain_usage.url,
LEFT(url,locate('?',url)) AS cleanURL
FROM
domain_usage
WHERE
MONTH(domain_usage.login_date) = (Now()) AND
YEAR(domain_usage.login_date) = (Now());
Returns no records, and no error ?
im trying to clean the query string from URL field.....
Any thoughts ?
Your date comparisons are wrong. you're basically trying
3 = '2013-01-18 10:36:00'
you need to compare apples to apples:
MONTH(domain_usage.login_date) = MONTH(now())
modify your WHERE condtion,
SELECT domain_usage.url, LEFT(url,locate('?',url)) AS cleanURL
FROM domain_usage
WHERE MONTH(domain_usage.login_date) = MONTH(CURDATE()) AND
YEAR(domain_usage.login_date) = YEAR(CURDATE())