Hi guys I have a mySQL database with a table called queue the rows consist columns singer and song. I'm using the table to keep the order the singers have gone up there is an auto increment Id
What I am trying to find is the proper syntax to copy the first row from my table to the last row with a new auto increment ID value and then delete the first row. It didn't seem such a challenge until I tried to write it . Any ideas are welcome.
Since getting an answer I have tried this.. but keep getting error 1111
UPDATE `queue`
SET `id` = MAX(id) + 1;
WHERE `id` = MIN(id);
Have also tried this...how can this be so hard.
Set $max = (SELECT MAX(id) + 1 FROM queue);
Set $min = (SELECT MIN(id) FROM queue);
UPDATE `queue`
SET `id` = $max
WHERE `id` = $min;
So I have abandoned the max() min() thought unless someone has a usable answer and moved to my original thought and I am very close. but my problem is I will not know the lowest id to remove because it would only be 1 once so I need to fill it in with a MIN(id) value or variable somehow. Here is where I am at.
INSERT INTO queue (SELECT NULL,singer, song1 FROM queue WHERE id = 1);
DELETE FROM queue ORDER BY id ASC LIMIT 1;
Why not just UPDATE the row to change its id by using a SET and WHERE clause?
UPDATE `queue`
SET `id` = 21 -- (current_highest_value + 1)
WHERE `id` = 1
This way you don't have to worry about adding a new row and deleting the old one.
i think all things are fine just you need to update SQL_SAFE_UPDATES = 0 for delete statement, so you can try below way
SET SQL_SAFE_UPDATES = 0;
INSERT INTO queue (SELECT NULL,singer, song1 FROM queue WHERE id = 1);
DELETE FROM queue ORDER BY id ASC LIMIT 1;
Related
This is my case:
What I want is to increment all rows after the raw of 135th by one, for e.g: 136,137.
I have been trying to make some updates but none of them works.
Can you please help me?
Thanks in advance
It fails because the consecutive ID's already exist. What you could do is first increasing you ID's to non-existing values, then decrease them to the values you want.
Example
Increase the ID's with a bigger amount than the highest ID, i.e.:
UPDATE tablename SET id = id + 100000 WHERE id >= 135;
Then decrease them to the values you wanted (in this case 100.000 minus 1)
UPDATE tablename SET id = id - 99999 WHERE id >= 100000;
Reset the auto increment
ALTER TABLE tablename AUTO_INCREMENT = 1;
Try
UPDATE `tablename` SET `id` = `id` + 1 WHERE `id` >= 135 ORDER BY `id` DESC
I want to put a entry in the database with id of 2. In order to do that I'm moving all records having more than 2 to id + 1.
UPDATE
`pedo_meta`
SET
`id` = `id` + 1
WHERE
`id` > 1;
Because id is a primary key. This query is failing saying :
#1062 - Duplicate entry '3' for key 'PRIMARY'. I know this is happening because query execution starts from top to bottom and when the query tries to make a the column having id 2 to id 3, a duplicate entry error occurs because id 3 column already exists.
I believe this problem can be solved if we reverse the order of execution. So the query starts by making id 537 to 538 then 536 to 537 and so on. So is there any to reverse the order of execution for a query in MySQL?
You can use ORDER BY on the UPDATE statement to affect the order the rows are updated:
If the ORDER BY clause is specified, the rows are updated in the order that is specified.
source: https://dev.mysql.com/doc/refman/8.0/en/update.html
So you can use the following UPDATE statement:
UPDATE `pedo_meta`
SET `id` = `id` + 1
WHERE `id` > 1
ORDER BY `id` DESC;
demo on dbfiddle.uk
Note: If you want to change the sort order of items you shouldn't use the ID column. You can add an additional numeric column to define a sort order. The ID column should only be use to identify a record in the table.
Here is a simple algorithm to accomplish that fast:
1. create a new column new_id
2. set new_id = id + 1
3. update id and set it to be id = new_id
4. remove column new_id when you are done
P.S. IF you want to sort your items after id is better to create a new column sort_order which can be updated extremely fast...
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 like to update the field "filepath" of my table "imagedata" of the last entry made?
UPDATE `imagedata` SET `filepath`='sdsd' WHERE `id` = MAX(imagedata.id);
Somehow my synax ist not right it says:
Invalid use of group function`
what do i do wrong?
UPDATE `imagedata`
SET `filepath`='sdsd'
order by id desc limit 1
Another alternative:
UPDATE `imagedata`
SET `filepath`='sdsd'
where id = (select * from (select max(id) from imagedata) as t)
If you use an AUTO_INCREMENT column, You could try
UPDATE `imagedata` SET `filepath`='sdsd' WHERE `id` = LAST_INSERT_ID();