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?
Related
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?
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.
I have two tables and both include 2 columns, sureness and kindness and and they are related to each other by dataitemID and id. Just to show you the structure I will put 2 selects that I got from database:
SELECT ID,sureness,kindness FROM omid.tweet ;
SELECT ID,DataitemID,sureness,kindness FROM omid.entity_epoch_data ;
and I want to copy all value of sureness and kindness in omid.tweet into entity_epoch_data where entity_epoch_data.entityID is equal to entityID coming from entity_relation where tweet.ID =entity_relation.ID
I want just to do it in mysql rather than reading the whole table in java and updating the database in the loop but I am so confused. How can I do that?I appreciate any help:)
Update:
I wrote the code as follow but it does not work:
update tweet, entity_epoch_data
set entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness ,
entity_epoch_data.calmness = tweet.calmness ,
entity_epoch_data.happiness = tweet.happiness
WHERE entity_epoch_data.EntityID in(
SELECT EntityID FROM omid.entity_dataitem_relation
INNER JOIN omid.tweet t ON entity_dataitem_relation.DataitemID = t.ID)
It's actually pretty straight forward. The UPDATE clause works like a JOIN and then use SET to set the values
UPDATE tweet INNER JOIN entity_epoch_data
ON tweet.id = entity_epoch_data.id
SET entity_epoch_data.sureness= tweet.sureness,
entity_epoch_data.kindness = tweet.kindness
I am trying to figure out how to update a row in one table, setting a column value equal to a value in a different table. Here's an example:
movies:
movie_id | movie_price
movies_attended:
attended_id | attended_movie_id | attended_movie_price
Now, this is kind of a stupid example, but supposed that for some reason there is a row in movies_attended that does not have the correct attended_movies_price in it and so it needs to be updated.
How should a query be written to update the movies_attended table, setting movies_attended.attended_movie_price = movies.movie_price?
I tried something similar to the following, but it did not work:
update movies_attended, movies
set movies_attended.attended_movie_price = movies.movie_price
where movies_attended.attended_movie_id = movies.movie_id
AND attended_id = [the id of the row we want to update]
When you say "it did not work", do you mean that it reported 0 rows updated, or did the statement cause the database raise an exception?
Your example statement appears to be of the form:
UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
SET a.attended_movie_price = m.movie_price
WHERE a.attended_id = ?
(We typically prefer the JOIN ... ON ... style syntax to the comma join operator and the join predicates in the WHERE clause.)
I have no explanation as to why this statement would "not work".
It's possible this would report 0 rows affected, if no rows satisfy the predicates. It would also report 0 rows affected if the rows that would be changed do not require any changes... that is, the existing value in attended_movie_price already matches the value being assigned to it.
Normally, before running an update statement like that, I write it as a SELECT first, and see what values are returned...
By replacing the UPDATE keyword with SELECT ... FROM, and removing the SET clause:
SELECT m.movie_price AS new_val
, a.attended_movie_price AS old_val
, a.attended_id
FROM UPDATE movies_attended a
JOIN movies m
ON a.attended_movie_id = m.movie_id
WHERE a.attended_id = ?
This is actually a bad database design. You don't need movie price in two tables.
But, if you just need this, it goes something along this:
UPDATE movies_attended
INNER JOIN
movies
ON movies_attended.attended_movie_id = movies.movie_id
SET movies_attended.attended_movie_price = movie.movie_price
I realized that i was using a varchar attribute as a index/key in a query, and that is killing my query performance. I am trying to look in my precienct table and get the integer ID, and then update my record in the household table with the new int FK, placed in a new column. this is the sql i have written thus far. but i am getting a
Error 1093 You can't specify target table 'voterfile_household' for update in FROM clause, and i am not sure how to fix it.
UPDATE voterfile_household
SET
PrecID = (SELECT voterfile_precienct.ID
FROM voterfile_precienct INNER JOIN voterfile_household
WHERE voterfile_precienct.PREC_ID = voterfile_household.Precnum);
Try:
update voterfile_household h, voterfile_precienct p
set h.PrecID = p.ID
where p.PREC_ID = h.Precnum
Take a look at update reference here.
Similarly, you can use inner join syntax as well.
update voterfile_household h inner join voterfile_precienct p on (h.Precnum = p.PREC_id)
set h.PrecID = p.ID
What if the subquery returns more than one result? That's why it doesn't work.
On SQL Server you can get this type of thing to work if the subquery does "SELECT TOP 1 ...", not sure if mysql will also accept it if you add a "limit 1" to the subquery.
I also think this is pretty much a duplicate of this question ("Can I have an inner SELECT inside of an SQL UPDATE?") from earlier today.
Firstly, your index on a varchar isn't always a bad thing, if it is not a key you can shrink how much of the field you index to only index say the first 10 chars or so.
Secondly, it won't let you do this as if it is a set that is returned it could break.