MySQL UPDATE Based off of values - mysql

I need a query to update rows but first check if there a specific number.
Example:
number = 10
If the row includes the number 10, don't update.
It needs to be just the query. Something like this.
SELECT `number` FROM `users`;
if row.number == 10 then don't update
else update set number=12

UPDATE users
SET number = 12
WHERE number <> 10

Related

MYSQL UPDATE query that does delete the row instead, if updated value meets condition

I'd like to make a one-query ask to database that will update records, but if one specific record after this change is equal to 0, I'd like to remove this row. Is it possible? Example:
UPDATE `table` SET `row1`=`row1`+5, `row2`=`row2`+30 IF (row1 after the update == 0) DELETE WHERE `primarycolumn`=1
Obviously, the IF (row1 after the change == 0) DELETE part is just pseudocode that does not work - is it possible to create a query that will make it work that way?
You don't need to update and then check if row1 = 0.
Delete with this condition:
DELETE FROM `table`
WHERE `primarycolumn` = 1 AND `row1` + 5 = 0
If the condition fails then nothing will be deleted.
Then do the UPDATE:
UPDATE `table`
SET `row1`= `row1`+ 5, `row2` = `row2` + 30
WHERE `primarycolumn` = 1
If the row was not deleted it will be updated, but if it was deleted then nothing will be updated.

How can i run a select and update query simultaneously from phpmyadmin

I have a scenerio. I have a field called card_no. Some entries are blank. We don't want to handle that. Some have 16 digit integers and some have the proper data I need. What I need is that I want to select all the records that are 16 digits integer and replace them with xxxxxxxxxxxxLAST4DIGITS (The credit card format). Can i do that via mysql?
You don't have to SELECT the rows.
You can apply an UPDATE to a subset of rows matching a condition.
UPDATE scenario
SET card_no = CONCAT('xxxxxxxxxxxx', RIGHT(card_no, 4))
WHERE LENGTH(card_no) = 16
Yes. But you have to have this field as char(4) as you might want to store a number 0002 as 0002 not just 2. So I would first update the datatype of the field and then update the field entries as desired.
-- update datatype
alter table `cards`
modify column `card_no` char(4);
-- update values in card_no
update `cards`
set `card_no` = LPAD(if(length(`card_no`) > 4, substr(`card_no`, -4, 4), `card_no`), 4, '0')
where `card_no` is not null;
The 'xxx' append operation you can do at application code side or else it will take extra memory in db.

UPDATE based on DISTINCT in same table

In my MySQL table I've 192 rows with the same value in the field number, the value is 548.
I need distinct update this 192 rows with new value calculate from rand function in MySQL.
Each row should have a different value calculated random.
I tried this solution but in update I've still duplicate rows with the same value ...
UPDATE `tbl`
SET number = FLOOR(100 +(RAND() * 150))
WHERE
EXISTS (SELECT DISTINCT number)
AND number = 548;
update tbl set number = FLOOR(100 +(RAND() * 150)) where number = 548;
No need to check for number and running DISTINCT. If number is not present, it will simply update nothing.
SQLFiddle Demo

How to update multiple rows with one query with MySQL?

I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?
UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)
If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;
Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)

Limiting table rows

How can I store only 10 rows in a MySQL table? The older rows should be deleted when a new row is added but only once the table has 10 rows.
Please help me
You could achieve this with an after insert trigger, delete the row where it is min date. e.g. DELETE FROM myTable WHERE myTimestamp = (SELECT MIN(myTimestamp) FROM myTable) but that could in theory delete multiple rows, depending on the granularity of your updates.
You could have an incrementing sequence, and always just delete the min of that sequence.
The question is why you'd want to do this though? It's a slightly unusual requirement.
A basic example (not validated/executed, I don't have mySQL on this particular machine) would look something like.
CREATE TRIGGER CycleOldPasswords AFTER INSERT ON UserPasswords FOR EACH ROW
set #mycount = SELECT COUNT(*) FROM UserPasswords up where up.UserId = NEW.UserId;
if myCount >= 10 THEN
DELETE FROM UserPasswords up where up.Timestamp = (SELECT min(upa Timestamp) FROM UserPasswords upa WHERE NEW.UserId = upa.UserId) AND NEW.UserId = up.UserId;
END
END;
You can retrieve the last inserted id when your first row is inserted, and store it in a variable. When 10 rows are inserted, delete the row having id < id of the first inserted record. Please try it.
first of all insert all values using your insert query
and then run this query
delete from table_name where (cond) order by id desc limit 10
must specify an id or time in one column