I am trying to update records based on a secondary statement but i dont know how to link them together.
UPDATE WEBSITE SET CMS_ID = 99
SELECT *
FROM website
WHERE is_scanned = 'yes'
AND cms_id =0
I want to update table website set the colum cms_id to 99 where all websites is_scanned = yes and cms_id = 0
will my query work?
This query doesn't work ?
UPDATE website SET cmd_id = 99
WHERE is_scanned = 'yes' AND cms_id = 0;
I think this accomplishes the goal you stated and should work in most databases:
UPDATE WEBSITE SET CMS_ID = 99
WHERE is_scanned = 'yes'
AND cms_id =0
The SELECT FROM is not required unless joins between different records or tables are important to the goal of the update. Here are some other examples for SQL Server
Related
I've been working on this problem, researching what I could be doing wrong but I can't seem to find an answer or fault in the code that I've written. I'm currently extracting data from a MS SQL Server database, with a WHERE clause successfully filtering the results to what I want. I get roughly 4 rows per employee, and want to add together a value column. The moment I add the GROUP BY clause against the employee ID, and put a SUM against the value, I'm getting a number that is completely wrong. I suspect the SQL code is ignoring my WHERE clause.
Below is a small selection of data:
hr_empl_code hr_doll_paid
1 20.5
1 51.25
1 102.49
1 560
I expect that a GROUP BY and SUM clause would give me the value of 734.24. The value I'm given is 211461.12. Through troubleshooting, I added a COUNT(*) column to my query to work out how many lines it's running against, and it's giving a result of 1152, furthering reinforces my belief that it's ignoring my WHERE clause.
My SQL code is as below. Most of it has been generated by the front-end application that I'm running it from, so there is some additional code in there that I believe does assist the query.
SELECT DISTINCT
T000.hr_empl_code,
SUM(T175.hr_doll_paid)
FROM
hrtempnm T000,
qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE 1 = 1
AND T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
AND (T175.hr_paym_type = 'd' OR T175.hr_paym_type = 't')
GROUP BY T000.hr_empl_code
ORDER BY hr_empl_code
I'm really lost where it could be going wrong. I have stripped out the additional WHERE AND and brought it down to just T166.hr_empl_code = T175.hr_empl_code, but it doesn't make a different.
By no means am I any expert in SQL Server and queries, but I have decent grasp on the technology. Any help would be very appreciated!
Group by is not wrong, how you are using it is wrong.
SELECT
T000.hr_empl_code,
T.totpaid
FROM
hrtempnm T000
inner join (SELECT
hr_empl_code,
SUM(hr_doll_paid) as totPaid
FROM
hrtpaytp T175
where hr_paym_type = 'd' OR hr_paym_type = 't'
GROUP BY hr_empl_code
) T on t.hr_empl_code = T000.hr_empl_code
where exists
(select * from qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
)
ORDER BY hr_empl_code
Note: It would be more clear if you have used joins instead of old style joining with where.
I'm trying to use the following code to update a column in each of my rows until none of them are 0 (The default value). Here's my code:
UPDATE PERSON
WHILE = 0
SET TEAM = 1
WHERE TEAM = 0;
No need for a while, also the syntax is wrong for plsql block
UPDATE PERSON --UPDATING TABLE PERSONS
SET TEAM = 1
WHERE TEAM = 0; --WHEN IT'S 0 IT UPDATES
The above query will do what you asked.
Should work for mysql and oracle.
Hope my answer helps you.
No need to loop SQL can handle this in a single command.
UPDATE Person SET Team = 1 WHERE Team = 0
Apologies if something similar has been asked before, currently I run 3 different update queries in order to get the desired result. The Queries are as follows,
UPDATE users SET enabled = false where username in (SELECT username
FROM users WHERE enabled = true AND lastaccess != 0 AND lastaccess
< (EXTRACT('epoch' FROM CURRENT_TIMESTAMP) - (86400*200))*1000 AND
username NOT LIKE ('admintest%'));
Once this query runs (disabling all users who haven't accessed the system in a certain period), then I run the following 2 Queries on the whole table,
update users set weeklypopupuse = 0;
update users set monthlypopupuse = 0;
These 2 queries then reset the weekly and monthly use to 0.
Now this works perfectly fine as per the requirements, however is there a more elegant way of writing all these 3 queries into a single query which gives the same result.
Any help or advice will be highly appreciated. Thanks in advance
update
after OP gave full structure, refactored to:
update users
set
weeklypopupuse = 0
, monthlypopupuse = 0
, enabled = case
when
enabled = true
AND lastaccess != 0
AND lastaccess < (EXTRACT('epoch' FROM CURRENT_TIMESTAMP) - (86400*200))*1000
AND username NOT LIKE ('admintest%')
then false
end
How to update query in sql column by comparing two tables ? This might be duplicated question, but yet still cannot solve my problem. Any help would be appreciated.
What i've tried so far, but error
UPDATE b SET b.STAMP = b.STAMP + 10 FROM TB_FWORKERSCH b,TB_FWORKERCN a
WHERE a.ISSDATE>='20150401' AND a.UKEY=b.UKEY2 and b.STAMP=0 AND b.IG_SUMINS!=0
DB2 Database
DB2 doesn't allow a JOIN or FROM for an UPDATE clause (this is also not specified in the SQL standard).
You can achieve what you want with a co-related sub-query:
UPDATE tb_fworkersch b
SET stamp = stamp + 10
WHERE EXISTS (SELECT 1
FROM tb_fworkercn a
WHERE a.issdate >= '20150401'
AND a.ukey = b.ukey2)
AND b.stamp = 0
AND b.ig_sumins <> 0
Try this:
MERGE INTO TB_FWORKERSCH b
USING TB_FWORKERCN a
ON a.UKEY=b.UKEY2
AND a.ISSDATE>='20150401' AND b.STAMP=0 AND b.IG_SUMINS<>0
WHEN MATCHED
THEN UPDATE SET b.STAMP = b.STAMP + 10;
I have the following sql statement and I want to update a field on the rows returned from the select statement. Is this possible with my select? The things I have tried are not giving me the desired results:
SELECT
Flows_Flows.FlowID,
Flows_Flows.Active,
Flows_Flows.BeatID,
Flows_Flows.FlowTitle,
Flows_Flows.FlowFileName,
Flows_Flows.FlowFilePath,
Flows_Users.UserName,
Flows_Users.DisplayName,
Flows_Users.ImageName,
Flows_Flows.Created,
SUM(CASE WHEN [Like] = 1 THEN 1 ELSE 0 END) AS Likes,
SUM(CASE WHEN [Dislike] = 1 THEN 1 ELSE 0 END) AS Dislikes
FROM Flows_Flows
INNER JOIN Flows_Users ON Flows_Users.UserID = Flows_Flows.UserID
LEFT JOIN Flows_Flows_Likes_Dislikes ON
Flows_Flows.FlowID=Flows_Flows_Likes_Dislikes.FlowID
WHERE Flows_Flows.Active = '1' AND Flows_Flows.Created < DATEADD(day, -60, GETDATE())
Group By Flows_Flows.FlowID, Flows_Flows.Active, Flows_Flows.BeatID,
Flows_Flows.FlowTitle, Flows_Flows.FlowFileName, Flows_Flows.FlowFilePath,
Flows_Users.UserName, Flows_Users.DisplayName, Flows_Users.ImageName,
Flows_Flows.Created
Having SUM(CASE WHEN [Like] = 1 THEN 1 ELSE 0 END) = '0' AND SUM(CASE WHEN [Dislike] = 1
THEN 1 ELSE 0 END) >= '0'
This select statement returns exactly what I need but I want to change the Active field from 1 to 0.
yes - the general structure might be like this: (note you don't declare your primary key)
UPDATE mytable
set myCol = 1
where myPrimaryKey in (
select myPrimaryKey from mytable where interesting bits happen here )
Because you haven't made your question more clear in what result you want to achieve, I'll provide an answer with my own assumptions.
Assumption
You have a select statement that gives you stuffs, and it works as desired. What you want it to do is to make it return results and update those selected rows on the fly - basically like saying "find X, tell me about X and make it Y".
Anwser
If my assumption is correct, unfortunately I don't think there is any way you can do that. A select does not alter the table, it can only fetch information. Similarly, an update does not provide more detail than the number of rows updated.
But don't give up yet, depending on the result you want to achieve, you have alternatives.
Alternatives
If you just want to update the rows that you have selected, you can
simply write an UPDATE statement to do that, and #Randy has provided
a good example of how it will be written.
If you want to reduce calls to server, meaning you want to make just
one call to the server and get result, as well as to update the
rows, you can write store procedures to do that.
Store procedures are like functions you wrote in programming languages. It essentially defines a set of sql operations and gives them a name. Each time you call that store procedure, the set of operations gets executed with supplied inputs, if any.
So if you want to learn more about store procedures you can take a look at:
http://www.mysqltutorial.org/introduction-to-sql-stored-procedures.aspx
If I understand correctly you are looking for a syntax to be able to select the value of Active to be 0 if it is 1. The syntax for something like that is
SELECT
Active= CASE WHEN Active=1 THEN 0 ELSE Active END
FROM
<Tables>
WHERE
<JOIN Conditions>