MySQL update only one record - mysql

I have a use case at which i need to update only one record based on different conditions, the problem is those conditions retrieve many record,
so the update statement update all of the records every time,
UPDATE t_wallet_log wallet_log
LEFT JOIN t_end_user enduser
ON enduser.f_id=wallet_log.f_end_user_id
set wallet_log.f_txn_status = 'successful'
WHERE enduser.f_ref_number='ugY-227'
AND wallet_log.f_amount=1000
AND wallet_log.f_txn_kind='cr'
I tried almost every thing i know but no luck

Please try below query.
UPDATE wallet_log set wallet_log.f_txn_status = 'successful' where id=(
select p1.id from ( select wallet_log.id from t_wallet_log wallet_log
LEFT JOIN t_end_user enduser
ON enduser.f_id=wallet_log.f_end_user_id
WHERE enduser.f_ref_number='ugY-227'
AND wallet_log.f_amount=1000
AND wallet_log.f_txn_kind='cr' order by id desc limit 1)p1);
This will help you.

Related

Error Code 1242: Subquery retuens more than 1 row

I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?

Update Query taking long to run

I need some assistance with a query I am using (MySQL) to update another table. When running the nested query, the query runs in less than a second. But as soon as I include the update part, it takes hours to run. The query I am using is as per the below:
UPDATE sys_reference.outlet_reference OUTREF LEFT JOIN
(SELECT
store_code 'storeCode'
, LEFT(header_value,20) 'CoOrds'
FROM
am_data_warehouse.am_headers
WHERE
action_date = CURDATE()- 1
AND header_field_id IN (3641, 4937)
) GPSCO
ON OUTREF.store_code = GPSCO.storeCode
SET OUTREF.gps_coordinates = GPSCO.CoOrds
Below is the structure of the table that is being updated:
I think the sub-query isn't doing you any favors here. I think you can rewrite it to elimiate the sub-query.
UPDATE
sys_reference.outlet_reference AS OUTREF
INNER JOIN am_data_warehouse.am_headers AS GPSCO ON OUTREF.store_code =
GPSCO.storeCode
SET
OUTREF.gps_coordinates = LEFT(GPSCO.header_value,20)
WHERE
GPSCO.action_date = CURDATE() - 1
AND GPSCO.header_field_id IN (3641, 4937)

MySQL - Update where fields are the same

The title probably doesn't say much useful, so I'll explain. Basically I have a first query:
UPDATE sales_records
INNER JOIN (SELECT buyer_full_name, buyer_address_1
FROM sales_records
GROUP BY buyer_full_name, buyer_address_1
HAVING COUNT(*)>1) multiorder
ON sales_records.buyer_full_name = multiorder.buyer_full_name
AND sales_records.buyer_address_1 = multiorder.buyer_address_1
SET processed=1
WHERE sales_records.buyer_full_name<>''
AND sales_records.buyer_address_1<>''
So the above sets everyone who has made more than one order as processed=1. However, in the same table there are parts of the order which do not have a buyer_address_1 or a buyer_full_name but do have the same sales_record_number field. I need to update the remaining ones with the same sales_record_number to processed=1 too.
This is what I've tried (amongst other similar variations) but it doesn't work:
UPDATE sales_records
WHERE sales_record_number=(SELECT sales_record_number
FROM ebay_sales_records
WHERE processed=1)
AND processed=1
Any ideas?

Update using Select Statement

I wanna write a query like this :
UPDATE `test_credit`
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5))
WHERE `test_credit`.`name` = `users`.`uname`
in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column
what is correct query ?
You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.
Give this a try,
UPDATE test_credit a
INNER JOIN users b
ON a.name = b.uname
SET a.credit = (a.credit - ((a.credit/100) * 5.0))
-- WHERE b.parent= "example"

how to update one table using data from second table

I am bit stuck with this one.. what i want is update app_name (first table). It returns no error.. but does nothing...
UPDATE tbl_m_app AS tma, tbl_measure AS tm
SET tma.app_name='Ap1'
WHERE (tm.mesure_id = tma.mesure_id
AND tm.live = 1)
This query will do the same work in more obvious way and without joins
UPDATE tbl_m_app AS tma
SET tma.app_name='Ap1'
WHERE tma.mesure_id IN (SELECT tm.mesure_id FROM tbl_measure AS tm WHERE tm.live = 1)
I think this SQL is fine, it's just not matching any rows.
Check this by using a select with the same where clause:
SELECT * FROM tbl_measure tm WHERE tm.live=1;
0 rows returned, right?