UPDATE based on DISTINCT in same table - mysql

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

Related

Beginner MySQL query build for find and replace

I'm just getting into SQL and need a hand with some of the basics.
I'm trying to write a query that will allow me to find values between range in one column of a table and replace all values in that range with a single value that I would specify with an UPDATE or REPLACE function.
I've gotten as far as
SELECT *
FROM (Table_A)
WHERE Column BETWEEN #a and #b
ORDER by Column desc
but I get back 0 results even though I know there are tons of values between #a and #b.
Select with BETWEEN
SELECT * FROM Table_A WHERE column BETWEEN 10 AND 30
Update
update Table_A set column = 'NEW_VALUE' where column BETWEEN 10 AND 30
Replace
// string hallo2 -> will be change to hallo3
update Table_A set column = replace(column, "2", "3") where column BETWEEN 10 AND 30

MySQL/SQLite insert same random # to multiple rows

MySQL/SQLite
I want to insert a randomly generated number (of 9 positions) into multiple rows BUT they need to be the same for all rows matched in the query.
update products set tag_seed=( SELECT ABS(RANDOM() % 999999999) ) where [...];
Partialy works... Each row will have a different random number. I need them to be same.
This is logical since you will generate a new random for every update query. The easiest solution is to generate a random integer, store it in a local variable and use that variable in your queries:
SET #rand := (SELECT ABS(RAND() * 1000000000));
update products set tag_seed=#rand where [...];

MySQL UPDATE Based off of values

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

Cleaning old data after inserting the new data into table

First we start with empty table
rows = 0
Second we insert random rows let say 3400
rows = 3400
For the third time i count how many rows are in the table, then insert the new rows and after that delete rows <= from the count.
This logic only work for the first time. If that repeat the count will always be 3400 but the id will increase so it will not delete the rows
I cant use last inserted ID since the rows are random and I dont how many it will load.
// Update
"SELECT count(*) from table" - the total count so far
"INSERT INTO tab_videos_watched (id , name) values (id , name)" - this is random can be 3400 or 5060 or 1200
"DELETE FROM table WHERE idtable <= $table_count"
If id is auto incremented, then you should use like:
select max(id) from my_table;
Read this maxId into a variable and then use when issued a delete query like:
delete from my_table where id <= ?;
Replace query parameter with the last found maxId value.
Alternatively you can define a column last_inserted as datetime type.
Before next insertions, select it into a local variable.
select max(last_inserted) as 'last_inserted' from my_table;
And after insertions are made, use the last_inserted to delete records.
delete from my_table where last_inserted <= ?;
Replace query parameter with the last found last_inserted value.

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