is this the correct way to optimize MySQL Query? - mysql

$sql="update users_contain
set
wood_max = (Select building_production from building_level where merge_id=$subPrimaryKey and empire_id=$user_empireID) ,
iron_max = wood_max,
clay_max = wood_max
where user_id = $user_id";
Now there is a question.
will wood_max will always be updated first than iron_max and clay_max. so it is safe to use this way??
i do not want to use inner query for updating the iron_max and clay_max when i know it has same value for all three fields..

According to this documentation, your UPDATE statement works as you want it to: http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-update.html
Test it to be sure, but I think you're fine.

Related

Using concat to put together a simple string, a subquery, and another simple string

I am needing to use concat to create custom sql statements.
I'm trying to use
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1), 'nam.jpg')
The goal is to update the image_loc in setuptable to "/file/to/image/UIDnam.jpg"
I'm getting Syntax errors and "Expected End of Statement".
How can i tie a simple string, a subquery, and another simple string together to update a field?
I found a way to make it work. If any one has any tips of improvement, I'm up to learn!
*I can't use variables
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1)+, 'nam.jpg')
I found slightly better following structure :
update setuptable
set image_loc = CONCAT('/file/to/image/', t1.UID, 'nam.jpg')
from table1 t1
I don't have mysql server at hand for the moment, so I have not tested.

Statement in trigger is not "picking up" the condition in its Where clause

so I'm currently working on a MySQL trigger. I'm trying to assign values to two variables when a new record is inserted. Below are the queries:
SET mssgDocNo = (SELECT Document_ID FROM CORE_MSSG WHERE Message_ID = new.MSSG_ID);
SET mssgRegime = (SELECT CONCAT (Regime_Type, Regime_Code) FROM T_DOC WHERE CD_Message_ID = new.MSSG_ID);;
For some reason, the second SQL query is not picking up the 'new.MSSG_ID' condition while the first query in same trigger recognizes it. I really can't figure out what seems to be the problem.
When I replace the 'new.MSSG_ID' with a hard-coded value from the database in the second query it seems to work. I doubt the 'new.MSSG_ID' is the problem because it works perfectly fine in the first query.
I've tried pretty much anything I could think of. Would appreciate the help.
I would write these more simply as:
SELECT mssgDocNo := Document_ID
FROM CORE_MSSG
WHERE Message_ID = new.MSSG_ID;
SELECT mssgRegime := CONCAT(Regime_Type, Regime_Code)
FROM T_DOC
WHERE CD_Message_ID = new.MSSG_ID;
The SET is not necessary.
I did make one other change that might make it work. I removed the space after CONCAT. Some versions of MySQL have trouble parsing spaces after function calls.

Changing column content in many rows

I have a column called code and in every row, the column contains FE. Because I do not want to go through 13,000 records, is there a quick way to replace FE inside 'code' with FEU?
While I appreciate this may be a simple question (or not?), I wasn't sure how to word it in order to find a solution.
You should be able to do like this:
UPDATE <table> SET code = REPLACE(code, 'FE', 'FEU');
If the column really just contains the value FE a simple WHERE clause should be enough:
UPDATE <table> SET code = 'FEU' WHERE code = 'FE';
Maybe something like this?
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'
This will work if your column contains only the string 'FE' and you want to replace it with 'FEU'.
update `table`
set `code` = replace(`code` , "FE","FEU")
where (if there is a where write it here)
should do you
This is probably the quickest way. You can try this:-
UPDATE yourtable
SET code = 'FEU'
WHERE code = 'FE'

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});

Update MySQL Column with WHERE condition

I need to change the information in a column to Degraded, where it is Feature Broken or Degradated. Is this the correct line?
UPDATE tablename
SET column = 'Degraded'
WHERE column = 'Degradated' OR column= 'Feature Broken'
Thanks
You may want to use an IN operator, to avoid the OR:
UPDATE tablename SET columnname = 'Degraded' WHERE columnname IN ('Degradated', 'Feature Broken');
Also, I would suggest running a SELECT first so you are somewhat aware of how many rows will be changed with your command.
Yes, it is.
You could make it more concise (but it is exactly the same otherwise)
...
WHERE column IN ('Degradated','Feature Broken')