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();
Related
I have a table in MYSQL database with two fields:
Id (auto increment field).
Post_Id.
When I insert a new record both fields should have the same value. So I should update post_id with Id value, and at the same time make sure that I update the field with the right value not with any other new inserted record value.
I tried this SQL statement but it was very slow and I was not sure that I select the right value:
set #auto_id := (SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='table_name'
AND TABLE_SCHEMA=DATABASE() );
update table_name set post_id= #auto_id where id=#auto_id ;
I don't have long experience with MySQL and I cannot change the table structure .
The approach you followed is not transaction safe as well.
The best option I can think about is to use trigger
Edit: According to #lagripe's mentionings
CREATE TRIGGER sometrigger
AFTER INSERT ON sometable
BEGIN
SET NEW.post_id := (SELECT id from sometable order by DESC limit 1) + 1 ; // you may need +1 here. I couldn't test it.
END
or you may consider to use LAST_INSERT_ID
insert into table_name values ( .... );
update table_name set post_id = LAST_INSERT_ID();
but why do you need two columns with the same id at first place?
if you really need why don't you use computed/generated columns?
CREATE TABLE Table1(
id DOUBLE,
post_id DOUBLE as (id)
);
you can use triggers :
CREATE TRIGGER UpdatePOST_id
BEFORE INSERT ON table_db
FOR EACH ROW
SET NEW.post_id := (select id from table_db order by id DESC LIMIT 1)+1 ;
from now on, whatever you insert your as a value in post_id column will be replaced with the id inserted automatically.
Test :
|id|post_id|
|20| 20 |
|21| 21 |
|22| 22 |
|23| 23 |
To drop the trigger :
DROP trigger UpdatePOST_id
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;
I've being trying to amend the solution found in this tutorial to write an SQL query that both SELECTs and UPDATEs my table:
enter link description here
DECLARE #column1 varchar(2);
SET #column1 = (SELECT `Id`, `Url` FROM `MyTable` WHERE `Retrieved` = 0);
SELECT * FROM `MyTable` WHERE `AdId`, `Url` = #column1;
UPDATE `MyTable` SET `Retrieved` = 1 where `Id`, `Url` = #column1;
What i'm trying to achieve the following simultaneously:
SELECT Id, Url FROM MyTable WHERE Retrieved = 0
UPDATE MyTable SET Retrieved = 1
for the rows where i have SELECTed the results from
Basically, i want to select all data from ID and Url columns where the Retrieved column equals 0. I then want to set the Retrieved column to 1 for the rows i have selected.
The "normal" SQL method would be:
UPDATE MyTable
SET Retrieved = 1
WHERE id IN (SELECT Id FROM MyTable WHERE Retrieved = 0);
That does not work in MySQL. Assuming that id is unique in MyTable (a reasonable assumption in my opinion), then this does what you want:
UPDATE MyTable
SET Retrieved = 1
WHERE Retrieved = 0;
UPDATE t SET t.Retrieved=1 FROM MyTable t WHERE t.Retrieved=0
This is only updating rows, that essentially you've selected. In your case you want to update rows where the selected rows Retrieved column is equal to 0.
The other thing and maybe for readibility or you need the rows returned you can use a cte.
--first get only the records you need
WITH MyRecords_cte
AS
(
SELECT Id, URL, Retreived FROM MyTable WHERE Retreived=0
)
UPDATE MyRecords_cte SET MyRecords_cte.Retreived=1
Once you're done with the update you can return the data.
Is it possible to set initial value for newly added column from another joined table?
Something like:
ALTER TABLE atable ADD COLUMN mycolumn VARCHAR(255) NOT NULL
VALUE SELECT acolumn FROM something s WHERE s.id = atable.some_id
?
I don't think what you want is possible. Instead, use a separate update command:
ALTER TABLE atable ADD COLUMN mycolumn VARCHAR(255);
update atable a join
something s
on s.id = a.some_id
set a.mycolumn = s.acolumn;
I'm trying to implement a custom sort field for a list of records. When I create a new record, by default I would like this field to match the ID number of that record. Is there any way to achieve this without having to perform two queries?
Any advice appreciated.
Thanks
You can get the next auto-increment id by using
SHOW TABLE STATUS FROM tablename LIKE Auto_increment
/*or*/
SELECT `auto_increment` FROM `INFORMATION_SCHEMA.TABLES` WHERE table_name = 'tablename'
This will give you the next auto_increment value.
Then make a before insert trigger:
DELIMITER $$
CREATE TRIGGER bi_table1_each BEFORE INSERT ON table1 FOR EACH ROW
BEGIN
DECLARE next_id integer;
SELECT `auto-increment` FROM `INFORMATION_SCHEMA.TABLES` INTO Next_id
WHERE TABLE_NAME = 'table1' LIMIT 1;
SET new.sortcolumn = next_id;
END $$
DELIMITER ;
Links
http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
http://dev.mysql.com/doc/refman/5.0/en/tables-table.html
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I think you can leave this field equal to NULL by default and then in your query do this:
ORDER BY ifnull(sort_field, id)
Set it to null or something like that by default and make your query sort by id if the sort field is null. PseudoSQL:
SELECT blah, IFNULL(t.sort, t.id) AS sortval
FROM t
ORDER BY sortval
You can use MAX(ID) expression to be sure that your next INSERT will be the greater id + 1 :
INSERT INTO table_name (..., sort_field) VALUES (..., MAX(id)+1)
Hope this helps, bye!