I am trying to update multiple values at the same time,
Table values
UniqueRef, Name
1101, AA01
1102, AA02
1103, AA03
I want to update UniqueRef for all of 1101, 1102 and 1103 to different values e.g
1101 will be updated to 1101AB ect
how can I do this in bulk than one at a time?
Do it in a single UPDATE statement:
Seems like you need to update UniqueRef in a single pattern. So use following statement:
UPDATE YourTable SET name = CONCAT(UniqueRef, 'AB');
This will update all rows - in case you need to limit the scope use WHERE statement and define appropriate condition.
In case you need to use different values for each row use CASE statement in
UPDATE YourTable
SET name =
CASE
WHEN UniqueRef = 1101 THEN 'newValue'
WHEN UniqueRef = 1102 THEN 'anotherNewValue'
{nextStatements}
ELSE name
END
;
The scalable solution is to use UPDATE with a JOIN on a source for the new values (usually another table, but it can be an online view, such as below)
UPDATE
example
INNER JOIN
(
SELECT 1101 AS id, 'X' AS new_name
UNION ALL
SELECT 1102, 'Y'
)
AS update_map
ON update_map.id = example.uniqueRef
SET
example.name = update_map.new_name
Demo : https://dbfiddle.uk/g71hwuYG
Related
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
);
As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles
I have a pure theoretical question, with a nonsense example:
UPDATE mytable
binaryData = '___GIANT_BINARY_DATA___',
isBig = LENGTH('___THE_SAME_GIANT_BINARY_DATA___') > 1000000000
WHERE id = 22
Now, if my binary data is a "gillion bytes", i want to avoid to write it twice in the plain SQL
UPDATE mytable
binaryData = '___GIANT_BINARY_DATA___',
isBig = LENGTH(binaryData) > 1000000000
WHERE id = 22
I want to update a column field, then re-use it, using its column name, in the same query
or maybe is there a way to define an alias in the UPDATE syntax, like i can do with SELECT?
thank you in advance
(p.s. i'm also interested in to the equivalent INSERT syntax)
You can use a CROSS JOIN like so:
UPDATE mytable a
CROSS JOIN (SELECT '__GIANT_BINARY_DATA__' AS bindata) b
SET a.binaryDate = b.bindata,
a.isBig = LENGTH(b.bindata) > 1000000000
WHERE a.id = 22
Which will give you access to that same value in every row, and you only have to pass in the data once in the SQL statement string.
MySql is an oddity in that the SET statements are non-atomic, meaning that as soon as one column is assigned a new value, that new value will be reflected if it is used elsewhere in the update statement.
The following statements:
CREATE TABLE Swap (
a CHAR(1),
b CHAR(1)
);
INSERT INTO Swap (a, b) VALUES ('a', 'b');
UPDATE Swap SET a = b, b = a;
SELECT * FROM Swap;
Will result in b, b in MySql, but b, a in every other RBDMS that I'm aware of...
So for your question, you don't need to alias binaryData, because soon as it is updated, the updated value will be reflected in the isBig assignment statement.
However, it's probably a bad idea to rely on this behavior, since it is non-standard.
You can use a user variable:
set #content = '___GIANT_BINARY_DATA___';
UPDATE mytable
SET binaryData = #content,
isBig = LENGTH(#content) > 1000000000
WHERE id = 22;
set #content = NULL; -- free up memory
I have a table in MySQL that looks like this:
studentID subjectID anotherID anotherID2, AnotherID3, studentScore
This table has about 100M records of students in it.
Suppose that I have the following information stored in two python lists:
listStudentIDs = [1, 123, 33, 5524, 425653]
listNewScores = [12.76, 73.2, 83.893, 92.3, 53.6]
Is it possible to have one query that will update all studentScore fields for all students (whose IDs are in listStudentIDs) if, and only if, their score in listNewScores is greater than their score currently stored in the database ( studentScore )?
In your example, they are 5 student ids for only 4 scores to update.
If your list of students to update is not to long, then you can have an update in one query like this :
UPDATE t_student
SET studentScore = CASE
WHEN studentID=1 THEN 12.76
WHEN studentID=123 THEN 73.2
WHEN studentID=33 THEN 83.893
WHEN studentID=5524 THEN 92.3
ELSE studentScore END
WHERE studentID IN (1, 123, 33, 5524, 425653)
Nevertheless less you may consider to use a SQL statement including several queries :
UPDATE t_student SET studentScore = 12.76 WHERE studentID = 1;
UPDATE t_student SET studentScore = 73.2 WHERE studentID = 123;
UPDATE t_student SET studentScore = 83.893 WHERE studentID = 33;
UPDATE t_student SET studentScore = 92.3 WHERE studentID = 5524;
Unfortunately, no, MySql can't handle this. Otherwise, I'd be using it all the time!
Try putting your two lists into a temporary table (studentID, studentScore).
Then you should be able to run a single query to update the main table accordingly.
UPDATE studentTable t1
JOIN tempTable t2 ON t1.studentID = t2.studentID
SET t1.studentScore = t2.studentScore WHERE t2.studentScore > t1.studentScore
Is there a way to update multiple rows with different values for each row using a single SQL query? I have to update one colum in many rows with different data. Using individual update queries for each row seems excessive so if it's possible I would like to consolidate this process into a single SQL statement or at least reduce the number of queries required.
I am using PHP with the Zend framework and MySql.
Create a temporary table and fill it with:
CREATE TEMPORARY TABLE temptable (id INTEGER, VALUE VARCHAR(200))
INSERT
INTO temptable
VALUES
('1', 'val1'),
('2', 'val2'),
('3', 'val3'),
('4', 'val4')
Then issue:
UPDATE
mytable m, temptable t
SET m.value = t.value
WHERE m.id = t.id
Don't know about MySQL specifically, but to update multiple rows based on a SELECT, or a UNION of multiple SELECTs, I would do
UPDATE U
SET MyColumn = T.OtherColumn
FROM MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
Update 10/28/2014, converted to work for MySQL:
UPDATE MyUpdateTable AS U
JOIN
(
SELECT [OtherColumn] = OtherColumn1
FROM MyOtherTable1
WHERE ...
UNION
SELECT OtherColumn2
FROM MyOtherTable2
WHERE ...
) AS T
ON T.ID = U.ID
SET MyColumn = T.OtherColumn
I know this works for SQL Server, so it's worth a try in MySQL.
update xtable
set a =
Case
when a = "a"
then z
when a = "b"
then y
End
where ...
You can construct the case statement based on your different rows.