UPDATE
mine
SET
reason = 'is this possible?',
deleted = 1,
WHERE id = '1'
GROUP BY date
I want to update 2 rows different id but same value, is this possible?
You can update as many rows as you want at the same time. For instance, to update ids 1 and 2:
UPDATE mine
SET reason = 'is this possible?',
deleted = 1
WHERE id IN (1, 2);
In fact, you can leave out the WHERE clause entirely and update all rows in the table.
I'm not sure why you have GROUP BY date, but date doesn't seem relevant to the query.
Related
I'd like to make a one-query ask to database that will update records, but if one specific record after this change is equal to 0, I'd like to remove this row. Is it possible? Example:
UPDATE `table` SET `row1`=`row1`+5, `row2`=`row2`+30 IF (row1 after the update == 0) DELETE WHERE `primarycolumn`=1
Obviously, the IF (row1 after the change == 0) DELETE part is just pseudocode that does not work - is it possible to create a query that will make it work that way?
You don't need to update and then check if row1 = 0.
Delete with this condition:
DELETE FROM `table`
WHERE `primarycolumn` = 1 AND `row1` + 5 = 0
If the condition fails then nothing will be deleted.
Then do the UPDATE:
UPDATE `table`
SET `row1`= `row1`+ 5, `row2` = `row2` + 30
WHERE `primarycolumn` = 1
If the row was not deleted it will be updated, but if it was deleted then nothing will be updated.
I am stuck with following statement. i dont know where the problem is. can someone please look into it and let me know, is it correct or not and if not than whats the correct one to update the row.
SELECT id FROM records WHERE user_id = 12119 AND field_id = 9
UPDATE records (user_id, field_id, value) VALUES (12119, 9, 'dallas')
You probably want to update value column for user with id 12119.
UPDATE records
SET value ='dallas'
WHERE user_id = 12119
AND field_id = 9;
use mysql update query like this
UPDATE table_name
SET column1=value, column2=value2
WHERE some_column=some_value
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
I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?
UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)
If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;
Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)
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