How to add value to a specific row in mysql? - mysql

I want to add weight value to row 2. How do I do that? I tried to do the following but it gives me an error:
INSERT INTO name(member_id, weight) VALUES(2,55.6);
What is the mistake that I'm having?

If you're updating a specific row you need to use the UPDATE command
UPDATE talbename
SET weight =55.6
WHERE member_id = 2

You can use the UPDATE clause and use a WHERE condition to target a particular row -
UPDATE fromis_9
SET weight = 2
WHERE member_id = 2;

INSERT INTO clause is used for inserting new records.
To Update an existing record, UPDATE clause is used.
UPDATE fromis_9 SET weight = 55.6 WHERE member_id = 2;

Hi I think you should use INSERT INTO table
where condition(member.id=2)
please refer to https://www.w3schools.com/sql/sql_insert_into_select.asp
Its better to learn how to read documentation than wait for complete answer ;)

Related

Copying One column from table to another table that has matching variables in another column

I hope I can explain this to make sense lol.
I am trying to copy variables from one hats_old.red to hats_new.red that match hats_new.name in both tables, if they do not match then i need it to do nothing so it does not null the value or set it to 0.
This is as far as ive gotten. This changes unmatched to 0 which i am trying to avoid and cannot figure the rest out.
This is for Mysql
Thank you
UPDATE hats_new
SET hats_new.red = (
SELECT hats_old.red
FROM hats_old
WHERE hats_old.name = hats_new.name LIMIT 1
);
An update with a join should do the trick:
UPDATE hats_new hn
JOIN hats_old ho ON hn.name = oh.name
SET hn.red = ho.red

Select column to update based on value

What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.

Update mysql cell after fetching related cell value via select?

SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.

Update MySQL column based on multiple values

How can I modify this statement to set the region value where there are multiple values in the location_town column?
UPDATE `wp_em_locations` SET `location_region` = 'The-Valley'
WHERE `location_town` = 'Bond'
IE: Bond is not the only town, I have many town names but all should be used to set location_region = The-Valley.
How do I accomplish this using one statement? Or do i need to run the same statement for each town name?
You can use either an OR or IN statement
WHERE `location_town` IN ('Bond', 'OtherTown')
or (no pun intended)
WHERE `location_town` = 'Bond' OR `location_town` = 'OtherTown'
Is the statement more complex than this? if you're trying to set every row to have location_region set to 'The-Valley' why use the where clause, which only serves to limit the scope of the update query?
Would this do what you're looking for, are am I misunderstanding?
UPDATE `wp_em_locations` SET `location_region` = 'The-Valley' WHERE 1;

Updating a table column based on another tables results using mysql only

I have a set of results - SELECT id FROM recruit_index WHERE YEAR NOT LIKE '2011'
I need to update another table column based on each of the above ids using mysql only.
Could anyone point me in the right direction?
Thanks in advance
Mike
Use:
UPDATE recruit_index, other_table set other_table.column={new value here}
WHERE recruit_index.id = other_table.id and recruit_index.year not like '2011';