I'd like to
UPDATE table SET column = 1 where column = 0;
INSERT (rows i just updated) INTO history_table;
Can I somehow store the ids from a select query, and then use those to UPDATE and subsequently INSERT rows matching those ids into the history table?
INSERT INTO history_table(id)
(SELECT id from table WHERE column = 0);
UPDATE table SET column = 1 where column = 0;
This way you are only getting the ID's that will be updated for the history_table and then you can update them to the correct values.
(I can't comment yet) Is there a specific reason to do it one query?
If not then you might use temporary table to store ids and fetch them for your update and insert using subquery.
Related
I have two table with column like this:
Table aggthndet (Reference table)
SELECT `aggthndet`.`idaggdet`,
`aggthndet`.`idagg`,
`aggthndet`.`noakun`,
`aggthndet`.`ketdet`,
`aggthndet`.`pagu`,
`aggthndet`.`prosesagg`,
`aggthndet`.`realisasi`,
`aggthndet`.`iu_id_usr`,
`aggthndet`.`iu_wkt`,
`aggthndet`.`iu_stat`FROM `aggthndet`;
Table aggakundet
SELECT `aggakundet`.`id`,
`aggakundet`.`idaggdet`,
`aggakundet`.`ketdetakun`,
`aggakundet`.`volume`,
`aggakundet`.`hrg_satuan`,
`aggakundet`.`iu_id_usr`,
`aggakundet`.`iu_wkt`,
`aggakundet`.`iu_stat`
FROM `aggakundet`;
The tables are mutually related to each other (relationship one-to-many)
i want insert data into table aggakundet, and update column pagu on table aggthndet, pagu column is the sum of the overall jml_total (alias column) of columns that have the same idaggdet.
sample data
table aggthndet
table aggakundet
In your script, you are having the value of idaggdet in $idaggdet.
Once insertion is done, proceed with UPDATE using the values in $idaggdet
You can take the below query as reference,
UPDATE `aggthndet`
SET `pagu` = `pagu`+1
WHERE `idaggdet` = '$idaggdet';
You can append this update statement in $sql itself.
I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);
I have two tables, Entries and mountPanels. What I want to do is when a record is inserted in mountPanels, sum up the panels in that table by the ID of the inserted record, and update the Entries table with that sum (that matches the same ID).
When I try the below AFTER INSERT trigger:
UPDATE Entries SET panels = (SELECT SUM(panels) FROM mountPanels WHERE Entries.EntryID = new.EntryID)
It sums up everything, but when I try
UPDATE Entries SET panels = (SELECT SUM(panels) FROM mountPanels WHERE Entries.EntryID = mountPanels.EntryID)
It sums up everything correctly by ID, but updates every row. I just want it to update the specific row with the ID that was entered last.
Your update statement doesn't have a where clause, so of course it's updating every row. Also, your statement doesn't seem to be using new correctly.
Try this:
UPDATE Entries SET
panels = (
SELECT SUM(panels)
FROM mountPanels
WHERE EntryID = new.EntryID)
WHERE EntryID = new.EntryID
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;