max(x,y) in MySQL - mysql

For the following table (all columns are integers)
[id, value, best_value]
For a given id and value I want update it's row setting the best_value column to max(newvalue,best_value). I seached into the documentation but I dont see a function for doing so.
Thanks

You want GREATEST(x,y). Example, if the new value is 530:
UPDATE my_table SET best_value = GREATEST(530,best_value) WHERE id=123

You don't strictly need any such function,
UPDATE my_table SET best_value = new_value
WHERE id=123 AND best_value < new_value
would do the job about as well as AlienWebguy's answer :)

Related

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 - UPDATE JOIN sets an unknown value "2147483647" to every entry

I want to update a whole table sav where the column phone contains the id from a table stock_phone. I want to set the sav.phonecolumn to the stock_phone.imei value, here is the query I tried:
UPDATE sav JOIN
stock_phone
ON sav.phone = stock_phone.id
SET sav.phone = stock_phone.imei;
But then the sav.phone value is set to 2147483647 for every row, and this value doesn't match with any imei value from stock_phone.
I search about how to UPDATE and JOIN but my syntax seems correct according to the questions I read.
Finally solved it myself..
I'm trying to set a too big integer into an INT(8) field. The 2147483647 value means Incorrect Integer, that's why the value isn't matching with one of my records.
Hope it will help some people.
2147483647 equals to 2^31-1 limit of INT(4). IMEI number (15 digits) are greater of this value.
I am curious if this returns the same results:
UPDATE sav s
SET s.phone = (SELECT sp.imei FROM stock_phone sp WHERE s.phone = sp.id);
I don't see a problem with updating the key used for the JOIN, but perhaps that is triggering some sort of bug.

Update column from a table with newly generated passwords

So I have the code for generating the kind of passwords I want which is
SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6) AS Paswoord
How do I make this work for all the 60 rows I already have at once.
I think it might be with Inner Join etc. I have tried some stuff but the all fail.
Thanks for reading.
UPDATE USER SET
PASSWORD = SUBSTRING(MD5(RAND(ID)) FROM 1 FOR 6)
WHERE PASSWORD IS NULL -- or whatever consition matches rows you want to update
Try this please: sorry made a typo - anyway the above two answers are there to help you :)
UPDATE maintable
SET maintable.column = (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6))
UPDATE tableName
SET columnName = SUBSTRING(MD5(RAND()),1,6)

mysql update of a field and consequent usage of it

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

MySQL Update a column value with the rowid of the group

I have a table of images, which are associated with a particular group (vehID) and I need to create their order for each group.
This is for an initial image entries position for a field I forgot to design in.
I have been playing with #row, but not getting very far.
set #row= 0;
select #row:=#row+1 as row, vehID,imgID from images group by vehID;
This gives me the rowID but does not reset for each group
Each vehID has 1 to n entries and I wish to calculate that value and update that rows' entry with the calculated value.
I can not work out how to reset #row to 0 on a change of vehID. Probably some sub-select.
If I can get Select to work any advice on how code the UPDATE if not obvious would be appreciated
Update images set imgPosition = Calculated Value where imgID = current imgID
you just need to declare another variable that holds the previous vehID. then using an IF statement you can compare the vehID with the previous one and set the row number accordingly. and then you set the previous vehID variable to be the vehID of the current row.
please note that I changed "group by" to "order by". this is for two reasons:
1- to list all rows
2- to make sure that rows from same id are following each other. otherwise comparing with previous value will not be correct.
and last, make sure you compare the previous vehID value and decide what the rownum is before you reset the previous variable to hold the value of the current line.
set #previous_vehID= 0;
set #row= 0;
select if(#previous_vehID=vehID, #row:=#row+1, #row:=1) as row,#previous_vehID:=vehID, vehID,imgID from images order by vehID
EDIT:
I just missed the update part. you can try cross table update for that:
set #previous_vehID= 0;
set #row= 0;
update images a, (
select if(#previous_vehID=vehID, #row:=#row+1, #row:=1) as row,#previous_vehID:=vehID, vehID,imgID from images order by vehID ) aa
set a.imgPosition = aa.row where a.vehID=aa.vehID and a.imgID=aa.imgID
the above makes the select and the update in a single statement/query. if not working, insert the results into a temporary table and use them to make an update in a separate statement.