i have a table in my MySQL database which i have added a new column to.
I would like to update this column on every row with a number starting at 20000 going up +1 each time.
i have tried this solution:
UPDATE table1 set new_col = new_col + 1;
but it just updates all rows with the same number
The easy way:
UPDATE table1 t, (SELECT #nr:= 20000-1) tmp
SET t.new_col = (#nr:=#nr+1) ;
I have used this query to solve this:
SET #rank:=20000;
update customer
set accountnumber_new=#rank:=#rank+1
Related
How do i update current row value if my previous row are same with current row.
example:
the curent row is 68, previous row is also 68.. i would i like to update current row become 68-20 which is 48.
same for 98-20 = 78.
so that the corrected data will look like:
i have more than 1000 record like this, which cant update the record one by one manually.
update table1 set DIH_QTY_BALANCE=DIH_QTY_BALANCE-DIH_REORDER_QTY
WHERE how to put the previous row same as current on where clause?
Here is the Schema + data:
http://pastebin.com/T1tYDT6Y
too large for sqlfiddle.
any help would be great.
As far as I remember, MySQL has problems to select from the same table in an update statement. And this is what you would have to do, because in order to update a record or not, you'd have to select its previous record from the same table.
So create a temporary table, give it row numbers, then select from it with a self join, to compare each record with its previous record.
create temporary table temp
(
rownum int,
dihistoryid int,
dih_qty_balance int
) engine = memory;
set #num = 0;
insert into temp
select
#num := #num + 1 as rownum,
dihistoryid,
dih_qty_balance
from mytable
order by dihistoryid;
update mytable
set dih_qty_balance = dih_qty_balance - dih_reorder_qty
where dihistoryid in
(
select current.dihistoryid
from temp current
join temp previous on previous.rownum = current.rownum - 1
where previous.dih_qty_balance = current.dih_qty_balance
);
drop temporary table temp;
May be something like this
SELECT DIH_QTY_BALANCE,
(SELECT DIH_QTY_BALANCE FROM example e2
WHERE e2.DIHISTORYID < e1.DIHISTORYID
ORDER BY DIHISTORYID DESC LIMIT 1) as previous_value,
(SELECT value FROM example e3
WHERE e3.DIHISTORYID > e1.DIHISTORYID
ORDER BY DIHISTORYID ASC LIMIT 1) as next_value
FROM example e1
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
I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;
One field of my table's field is set to 0 for all rows. But I want to update the incremental value by step 1 in an update query.
How can I do that in mysql?
Try this:
mysql> select #i := 0;
mysql> update bar set c = (select #i := #i + 1);
SET #a = 0;
UPDATE customers SET id = #a:=#a+1;
You can go for this as well.
One line solution:
UPDATE tablename AS a, (SELECT #a := 0) AS b SET a.fieldname = #a:=#a+1
One way is to create a new table with an AUTO_INCREMENT column instead of the original column, inserting all data from the old into the new table, and then renaming the new and deleting the old.
Another way is to run your update query with a MySQL variable that generates an increasing number for each row (to emulate the ROW_NUMBER() function found in other DBMS systems).
The easiest way for me is to drop the field and re-create it with auto-increment.
I want to do all these update in one statement.
update table set ts=ts_1 where id=1
update table set ts=ts_2 where id=2
...
update table set ts=ts_n where id=n
Is it?
Use this:
UPDATE `table` SET `ts`=CONCAT('ts_', `id`);
Yes you can but that would require a table (if only virtual/temporary), where you's store the id + ts value pairs, and then run an UPDATE with the FROM syntax.
Assuming tmpList is a table with an id and a ts_value column, filled with the pairs of id value, ts value you wish to apply.
UPDATE table, tmpList
SET table.ts = tmpList.ts_value
WHERE table.id = tmpList.id
-- AND table.id IN (1, 2, 3, .. n)
-- above "AND" is only needed if somehow you wish to limit it, i.e
-- if tmpTbl has more idsthan you wish to update
A possibly table-less (but similar) approach would involve a CASE statement, as in:
UPDATE table
SET ts = CASE id
WHEN 1 THEN 'ts_1'
WHEN 2 THEN 'ts_2'
-- ..
WHEN n THEN 'ts_n'
END
WHERE id in (1, 2, ... n) -- here this is necessary I believe
Well, without knowing what data, I'm not sure whether the answer is yes or no.
It certainly is possible to update multiple rows at once:
update table table1 set field1='value' where field2='bar'
This will update every row in table2 whose field2 value is 'bar'.
update table1 set field1='value' where field2 in (1, 2, 3, 4)
This will update every row in the table whose field2 value is 1, 2, 3 or 4.
update table1 set field1='value' where field2 > 5
This will update every row in the table whose field2 value is greater than 5.
update table1 set field1=concat('value', id)
This will update every row in the table, setting the field1 value to 'value' plus the value of that row's id field.
You could do it with a case statement, but it wouldn't be pretty:
UPDATE table
SET ts = CASE id WHEN 1 THEN ts_1 WHEN 2 THEN ts_2 ... WHEN n THEN ts_n END
I think that you should expand the context of the problem. Why do you want/need all the updates to be done in one statement? What benefit does that give you? Perhaps there's another way to get that benefit.
Presumably you are interacting with sql via some code, so certainly you can simply make sure that the three updates all happen atomically by creating a function that performs all three of the updates.
e.g. pseudocode:
function update_all_three(val){
// all the updates in one function
}
The difference between a single function update and some kind of update that performs multiple updates at once is probably not a very useful distinction.
generate the statements:
select concat('update table set ts = ts_', id, ' where id = ', id, '; ')
from table
or generate the case conditions, then connect it to your update statement:
select concat('when ', id, ' then ts_', id) from table
You can use INSERT ... ON DUPLICATE KEY UPDATE. See this quesion: Multiple Updates in MySQL
ts_1, ts_2, ts_3, etc. are different fields on the same table? There's no way to do that with a single statement.