sql - Possible to update two rows with different critera in one query? - sql

This is for SqlCe,
I am trying to update a table and set won +=1 for a winner, and lost =1 for the loser.
I know I can do this with two different update statements but I was wondering if I could make update the winners "won" value at the same time that I update the losers "lost" value.
Basically just looking like this,
UPDATE player SET won = won +1 WHERE id = 0
UPDATE player SET won = lost +1 WHERE id = 1

This isn't pretty but it works
UPDATE player SET won = won + CASE WHEN id = 0 THEN -1 ELSE 1 END WHERE id in (0,1)
I'd personally stick with the two update statements

Related

Is count() being used correctly for my query?

Find all teams that only won 1 game in tourney #3 (1 column, 4 rows)
My thought process to create this query is that I need to count (WonGame)s for each Team. And that that number cannot be has to equal 1. But When I run my query I get no results (I should get 4 teams).
Experimenting with my query I changed the equals to a greater than and that returned 8 results. So I don't understand why equals 1 returns no results.
Also I checked my Data and there is indeed 4 teams that one only one game during Tournament #3.
select Teams.TeamName
from Teams
join Bowlers on Teams.TeamID = Bowlers.TeamID
join Bowler_Scores on Bowlers.BowlerID = Bowler_Scores.BowlerID
join Match_Games on Bowler_Scores.GameNumber = Match_Games.GameNumber
join Tourney_Matches on Match_Games.MatchID = Tourney_Matches.MatchID
where Tourney_Matches.TourneyID = 3
group by Teams.TeamName
having count(Bowler_Scores.WonGame) = 1;
Bowling League DB Structure
Bowling League Data
The diagram seems to indicate that the relationship between Match_Games and Bowler_Scores is on BOTH of MatchID and GameNumber
If you change your JOIN conditions to be both columns
join Match_Games on Bowler_Scores.GameNumber = Match_Games.GameNumber and Bowler_Scores.MatchID = Match_Games.MatchID
Then you might get the required answer.
I can only speculate on what your data really looks like. However, it is doubtful that this expression:
having count(Bowler_Scores.WonGame) = 1;
does what you want. This counts the number of non-NULL values. Presumably, WonGame as some value such as "1" or "W" for the winner. If the value were 1, then the correct expression would be:
having sum(Bowler_Scores.WonGame) = 1
This is just speculation though without a better description of your data.
EDIT:
Based on the comment:
having sum(convert(int, Bowler_Scores.WonGame)) = 1

SQL Update each row until none of them are 0

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

Trouble Updating Field on a Particular Row

I am trying to update a record in a particular existing row of data.
EX: Existing Output; Team, Year, Player, Position
LA Lakers 2016 Jim Bowen
Can someone provide me a sample of the code to update 'Position' column
Update LAL_Player_Stats
Set POSITION = C
Where Player = 'Bowen'
This returns: (0 row(s) affected)
You may have to try Player = 'Jim Bowen'.
Position should not be all caps.
Also, you need single-quotes on the "C": Set Position = 'C'.
Making your final statement:
Update LAL_Plater_Stats
Set Position = 'C'
Where Player = 'Jim Bowen'
Update LAL_Player_Stats SET position = 'C' WHERE player = 'Jim Bowen'
In the future, why not provide us the whole table so we can determine what you're missing when you update a row. It might help us more. :)

SQL Noob compare table in opencart

I have one table called VEGAN with model numbers that i want 3800 rows with model or SKU codes. thats it.
In the main oc_products table there are 15000 products / rows with over 20 columns, In one of those columsn there is a control for enabled which can be set to 0 or 1.
I want to Disable all products in the main oc_products table if they are not in the VEGAN table, or Disable(set status to 0) them all first and Enable them (set status to 1) if they are in the Vegan Table.
So far I have come up with this... I am a first timer here.
FIRST DISABLE EVERYTHING...
UPDATE oc_product
SET status= '0'
WHERE 1
then...
UPDATE oc_product SET status= '1' WHERE 'ocproduct.model' = ‘VEGAN.SKU'
It doesn't work and Im stuck,
please help.
Try this query...
update oc_product set status = 1 where model in (select sku from vegan)
This assumes that oc_product.model matches vegan.sku.
Any vegan.sku with a matching oc_product.model will get oc_product.status set to 1.
For your second update statement you should do something like:
UPDATE oc_product SET status = '1'
WHERE ocproduct.model IN (SELECT SKU FROM Vegan)
or you can use the alternate format:
UPDATE oc_product SET status = '1'
FROM oc_product
INNER JOIN Vegan
ON oc_product.model = Vegan.SKU

How to update many rows with random values

I would like to perform update query to table and set 2 columns to random values. Here is an example:
Update Network_Info_Detail set ART = (rand()*4000)+2, NRT = (rand()*1000)+2
It's obvious that all rows will be updated with the same value randomly generated so I needed to create a cycle to generate a random value for each row.
DECLARE #size integer
SET #size = (SELECT Count(*) from Network_Info_Detail)
While #size > 1
BEGIN
Update top (#size) Network_Info_Detail set ART = (rand()*4000)+2, NRT = (rand()*1000)+2
SET #size = #size - 1
END
This script updates rows with different numbers, but it's very slow. Is there a way to improve the execution time?
You can use a SQL Server trick to generate a random number: rand(checksum(newid())).
Update Network_Info_Detail
set ART = (rand(checksum(newid()))*4000)+2,
NRT = (rand(checksum(newid()))*1000)+2;