Update table values from another table in sql - mysql

I have a temporary table havinf 2 colums and i need to update this data into another table
Can Some one please help me with this

You want to use join, something like this:
update othertable t join
temporarytable tt
on t.clientid = tt.clientid
set t.checklist = tt.checklist;

Related

How to match and insert new table column to a previous database column

https://imgur.com/a/0443EiL
Above is the image of what I am working with.
I need to insert the Styletype column so that the StyleYype matches up with the StyleCode column.
seems you need update based on join
update table1
inner join table2 ON table2.style = table1.styleCode
set table1.styleType = table2,styleType

Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement

I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);

MySQL JOIN INSERT

I have tree table
a (a_col1, a_col12, a_col3)
b (b_col1, b_col12, b_col3)
c (c_col1, c_col12, c_col3)
I want to write the b.b_col3 to c.c_col3
where a.a_col1 equals to b.b_col12.
What am I doing wrong ?
INSERT INTO c(c_col3)
SELECT a.a_col1, b.b_col12
FROM a LEFT JOIN b
ON
a.a_col1 = b.b_col12;
You are trying to insert 2 columns value in single column, use something like below-
INSERT INTO c(c_col2,c_col3) SELECT a.a_col1, b.b_col12 FROM a LEFT JOIN b ON a.a_col1 = b.b_col12;
You can not do both stuff with one query. You cannot INSERT and SELECT at the same time. Try first selecting and then inserting if it is possible.

Delete values in one table based on values in another table

I was trying to execute this statement to delete records from the F30026 table that followed the rules listed.. I'm able to run a select * from and a select count(*) from with that statement, but when running it with a delete it doesn't like it.. it gets lost on the 'a' that is to define F30026 as table a
delete from CRPDTA.F30026 a
where exists (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
Thanks!
This looks like an inner join to me, see MySQL - DELETE Syntax
delete a from CRPDTA.F30026 as a
inner join CRPDTA.F4101 as b on a.IELITM = b.IMLITM
where substring(b.IMGLPT, 1, 2) not in ('FG', 'IN', 'RM')
Please note the alias syntax as a and as b.
Instead of the 'exists' function, you can match the id (like you do in the where clause):
delete from CRPDTA.F30026 a
where a.IELITM IN (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
I believe this is what you really want, all IELITMs which meet your criteria.
delete from CRPDTA.F30026
where IELITM IN (
select IMLITM from CRPDTA.F4101
where substring(IMGLPT,1,2) not in ('FG','IN','RM'));

Sql :Subtraction formula for different Columns?

Im trying to write a formula in SQL Server to subtract the values of two columns which are [AmountSpent]column of table2 and [Amount] column of table1of two different tables and update the balance amount in [Amount] column any idea ?
Thank you in advance...
I think the following will work, assuming you have a reliable foreign key relationship between the two tables
UPDATE [table1]
SET [table1].[Amount] = [table1].[Amount] - [table2].[AmountSpent]
FROM
[table1] INNER JOIN
[table2] ON
[table1].[KeyField] = [table2].[KeyField]
i got it.........thanks #james Osborn
create procedure SP_Subtraction
(
#EmpID int
)
as
begin
UPDATE PTS_BalanceTracker
SET PTS_BalanceTracker.Balance_BalanceAmount = PTS_BalanceTracker.Balance_BalanceAmount - PTS_Transactions.Transaction_Amount
FROM
PTS_BalanceTracker INNER JOIN
PTS_Transactions ON
PTS_BalanceTracker.Emp_ID = #EmpID
end