How do I modify the hundards of records based on my query result? - sql

I inserted some error record to my table, I can query via the sql below and found hundards of record marked with half.
SELECT * FROM `marathon`
WHERE gender = 'male' && distance = 'half';
How to write a SQL then I can modify the result rows distance from 'half' to 'full'.
Thank you.

update marathon
set distance = 'full'
WHERE gender = 'male' and distance = 'half';
read http://dev.mysql.com/doc/refman/5.0/en/update.html (mysql update syntax)

Thierry has the right answer, but I want share a technique I use when writing update statements:
update m
set distance = 'full'
--select marathon_id, gender, distance from
From marathon m
WHERE gender = 'male' and distance = 'half';
By embedding the select in the stamenet as a comment, you can check to see which records will be affected before running the update and if you are updating to a complex formula you can even see what the current and updated results would be. Even better if you already have the select statment, it's pretty easy to add the update parts on top of it. This also helps you see how your update relates directly to your select.

Related

UPDATE or SET from a SELECT query?

I have a database of information that should only be used one time. To grab the information to be exported into a .CSV file for use. I would use the following query, we also have the column USED which should be set to YES after the data has been exported (so it not re-used).
SELECT TOP(40000)
ms.website AS Website,
ms.company AS COMPANY,
ms.address AS [ADDRESS],
ms.city AS CITY,
ms.state AS [STATE],
ms.zip AS ZIP,
ms.phone AS PHONE
FROM
[QUETABLE] as ms
WHERE
DEAD != 'YES' AND USED != 'YES';
And then I figured I would use this query to update the column "USED" so that if the same query was run again, only new information would be exported:
UPDATE TOP(40000) QUETABLE
SET USED = 'YES'
WHERE USED = 'NULL' AND DEAD != 'YES';
However, while a lot of the data was the same, for some reason, not all 40,0000 columns were matching, meaning un-used data would be marked as used (and vice versa), the USED column is set to NULL before its used (not IS NULL, but written "null').
How could I run the top query, but at the same time also set USED to "YES" so the information is identical? So, in broken SQL it would be like:
SELECT TOP(40000)
ms.website AS Website,
ms.company AS COMPANY,
ms.address AS [ADDRESS],
ms.city AS CITY,
ms.state AS [STATE],
ms.zip AS ZIP,
ms.phone AS PHONE
FROM [QUETABLE] as ms
WHERE DEAD != 'YES' AND USED != 'YES' THEN SET USED = 'YES';
But of course, that wouldn't work, I'm not sure how to accomplish this!
Thank you
You are using TOP with no ORDER BY. This returns an arbitrary set of rows. There is no reason to think that the rows returned on two different runs of the same query would return the same rows, much less an UPDATE and a SELECT.
I would suggest that you use the OUTPUT clause and do the work in the opposite order:
DECLARE #t TABLE ( . . . ); -- fill in the columns
UPDATE TOP(40000) QUETABLE
SET USED = 'YES'
OUTPUT inserted.* INTO #t;
WHERE USED = 'NULL' AND DEAD <> 'YES'
Now you can "export" the ones that were just set to DONE.

Sql Error Newbie

I am trying to update the number stored in a field,this is the code I have used
Select *
From MEDICATION
UPDATE medication
set seq_number = 2
where pet_id = "PO145"
AND vet_id = "V01"
AND MEDICINE ='Soothing Cream';
The error returned states
ORA-00933: SQL command not properly ended
As #Tim mentioned, select and update should be different. So run them individually.
Also the strings should be enclosed in single quotes, not double. Although this is not the reason for error, but it will not work for you with double quotes. Double quotes should be used for object/column names.
Also it is a good practice to run the where clause used in update or delete, with select first, as it will let you see what rows are returned, which will be updated or deleted.
UPDATE medication
set seq_number = 2
where pet_id = 'PO145'
AND vet_id = 'V01'
AND MEDICINE ='Soothing Cream';
It looks like you're trying to do a SELECT and an UPDATE at the same time. You can't do that. For the update, just use the latter part of your query by itself:
UPDATE medication
SET seq_number = 2
WHERE pet_id = "PO145" AND
vet_id = "V01" AND
MEDICINE = 'Soothing Cream';
After the update, if you want to do a SELECT * FROM medication then this should be no problem.

Find multiple SQL columns and update based on defined data listed in query

I have an update query in which I am trying to locate data in a column from a single table. All while taking other defined data listed in the query to update another column in the same table once a match has been found with that original search. Below is an example of my update statement. My end goal is to find '003447710' then update AltId to '540112'
UPDATE Site
SET AltId = ('540112'
'540129'
'540142'
'540143')
WHERE CCMFStatus in ('003447710',
'002754540',
'003564370',
'005942870')
I am sure there may already be something like this out there but I am really having trouble on an easy method on how to do this quickly and accurately.
Try this
update site
set altid = a.altid
from
(select altid,CCMFstatus from site) as a
where site.CCMFstatus = a.CCMFstatus
The best way might be multiple update statements:
UPDATE Site
SET AltId = '540112'
WHERE CCMFStatus = '003447710';
And so on.
If not, you can do this with a giant case statement or a join:
WITH values as (
SELECT '003447710' as oldstatus, '540112' as newaltid UNION ALL
SELECT '002754540', '540129' UNION ALL
SELECT '003564370', '540142' UNION ALL
SELECT '005942870', '540143'
)
UPDATE s
SET AltId = va.newaltid
FROM site s JOIN
values v
ON s.CCMFStatus = v.oldstatus;
If you already have the values in a table, then you don't need the WITH statement. You can just use the table.
Have you tried using CASE statement?
UPDATE SITE SET AltID = (CASE
WHEN CCMFStatus = '003447710' THEN '540112'
WHEN CCMFStatus = '002754540' THEN '540129'
END)
WHERE
CCMFStatus in ('003447710', '002754540', '003564370', '005942870');
BR,

Oracle SQL update

I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;

TRANSACT SQL - How to manually order/index a table based upon a select/where statement

I have sql puzzler today.
I have the following sql :
SELECT MemberId,
UnitId,
MaterialLevel,
MaterialText,
ProductPercentage,
ProductPriority
FROM tblWeights
ORDER BY MemberId, MaterialLevel, MaterialText, ProductPercentage DESC
Now, currently the ProductPriority is empty. What I would like to do is update this so that the first product, per level, per materialtext with the highest product percentage with "1", the second highest percentage with "2", etc, etc.
However, when the materialtext changes, this should reset itself and and start again at "1".
Can anyone think how I can do this?
Is there any reason you want productpriority explicitly stored in the database? Sorry if I've misunderstood your question but this sounds like it could be handled with a straight ROW_NUMBER in the output query.
You can use this for the initial update:
UPDATE tblWeights w1
SET ProductPriority = (
SELECT COUNT(*)
FROM tblWeights w2
WHERE w2.level = w1.level
AND w2.materialText = w1.materialText
AND w2.ProductPercentage >= w1.ProductPercentage
)
For the automatic reset, you should probably write a trigger that fires at the statement level after DELETE, UPDATE and INSERT. You can use a modified version of this statement, and only update the rows having the affected level,materialtext combination
CREATE TRIGGER aiud_tblWeights ON tblWeights
FOR INSERT, UPDATE, DELETE
AS
IF( UPDATE (Level) OR UPDATE (MaterialText) )
BEGIN
UPDATE tblWeights w1
SET ProductPriority = (
SELECT COUNT(*)
FROM tblWeights w2
WHERE w2.level = w1.level
AND w2.materialText = w1.materialText
AND w2.ProductPercentage >= w1.ProductPercentage
)
WHERE (w1.level, w1.materialText) IN (
SELECT level, materialText
FROM inserted
UNION ALL
SELECT level, materialText
FROM deleted
);
END;
I know its too late. but I found one answer when I was looking for similar questions for my help. see if helps someone else.
SQL query results to be displayed in the order of the manually provided values