How do I update all MySQL table rows at the same time?
For example, I have the table:
id | ip | port | online_status |
1 | ip1 | port1 | |
2 | ip2 | port2 | |
3 | ip3 | port3 | |
4 | ip4 | port4 | |
5 | ip5 | port5 | |
I'm planning to create cronjob and monitor some servers, but I don't know exactly how to update them all from the table at the same time. What are some examples on how to do that?
Omit the where clause:
update mytable set
column1 = value1,
column2 = value2,
-- other column values etc
;
This will give all rows the same values.
This might not be what you want - consider truncate then a mass insert:
truncate mytable; -- delete all rows efficiently
insert into mytable (column1, column2, ...) values
(row1value1, row1value2, ...), -- row 1
(row2value1, row2value2, ...), -- row 2
-- etc
;
update mytable set online_status = 'online'
If you want to assign different values, you should use the TRANSACTION technique.
The default null value for a field is "not null". So you must set it to "null" before you can set that field value for any record to null. Then you can:
UPDATE `myTable` SET `myField` = null
UPDATE dummy SET myfield=1 WHERE id>1;
You can try this,
UPDATE *tableName* SET *field1* = *your_data*, *field2* = *your_data* ... WHERE 1 = 1;
Well in your case if you want to update your online_status to some value, you can try this,
UPDATE thisTable SET online_status = 'Online' WHERE 1 = 1;
Hope it helps. :D
just use UPDATE query without condition like this
UPDATE tablename SET online_status=0;
Just add parameters, split by comma:
UPDATE tablename SET column1 = "value1", column2 = "value2" ....
see the link also
MySQL UPDATE
Related
I want to add a new record in a table if duplicate value enters in a unique field. I don't want to update the existing one but want to add a new record by modifying the unique field value.
Is this possible in mysql?
EDIT:
Edited after user comment on this post:
You need write table locking on both of those two processes.
A WRITE lock has the following features:
The only session that holds the lock of a table can read and write data from the table.
Other sessions cannot read data from and write data to the table until the WRITE lock is released.
Also look at SQL UNIQUE Constraint
BEFORE EDIT:
Yes it is possible. And it took me awhile to figure it out. I build this on your input and compering values as test1, test2 etc, where test is always the same and has trailing number. As you specified.
It can be done as MySQL TRANSACTION in 4 steps.
Lets say you have table testT where name is unique to insure we have no doubles.
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
And you want to insert a new item with name test1 we set is as:
SET #newName = 'test1';
Then we need to check if it already exists in table:
SELECT #check:=COUNT(*) FROM testT WHERE name = #newName;
We do a count here to get true or false and save it as #check here so we can compare it later. This will result into 1 row as test1 already exists in table.
Next we do another selection to get the highest number of test* and store it as #number, this next query selects all tests and does a SUBSTRING after 4 latter's giving us all numbers after first 4 latter's. (99999999999) numbers actually just to be sure we don't miss any but in our case result is only "3" because that is last record "test3" in table.
SELECT
#number:= SUBSTRING(name,5,99999999999)
FROM testT;
Now we can do an insert:
INSERT INTO testT(name)
VALUES
(
IF(#check = "", #newName , CONCAT(LEFT(#newName,4),RIGHT(#number,1)+1)
)
);
This tries to insert our #newName into table under IF condition, and that is if our #check is empty then he will insert #newName, if not it will take word test out of string and append a highest #number from earlier and add + 1 too it.
So result for #newName = 'test1' is below. If you change this into #newName = 'test3' result wold be same new insert test4.
**Schema (MySQL v5.7)**
SET #newName = 'test1';
---
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test4 |
---
And if you change it in ANY test* that number does not already exists it will insert it normally. In case below: #newName = 'test6'
SET #newName = 'test6';
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test6 |
This way an insert will always be made.
You can play with this here : View on DB Fiddle just by changing SET #newName = 'test6'
I am no expert and it took me couple of hours to figure this way out, as I wanted to know if this was even possible.
And I would appreciate if any other user can suggestion any other way or improve my method.
I need to do an update query making an md5 hash from my google calendar column. This is my query:
UPDATE `ea_appointments` SET `hash` = MD5(`id_google_calendar`)
Would this work to make something like this?:
Table: ea_appointments
id_google_calendar Hash
e5e3were760lkj792c7t5vm61bvk_20160729T200000Z d5f9f4ef02e438d49c8bf39cd4b4118d
Yes, it'll be work.
And you can loosely check it by:
select md5('test');
Result:
+----------------------------------+
| md5('test') |
+----------------------------------+
| 098f6bcd4621d373cade4e832627b4f6 |
+----------------------------------+
Or:
select md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z');
Result:
+------------------------------------------------------+
| md5('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z') |
+------------------------------------------------------+
| 06b5a13d9a7b0ed26ab1406434954972 |
+------------------------------------------------------+
Or:
create table t(id_google_calendar varchar(100), hash varchar(100));
insert into t values ('e5e3were760lkj792c7t5vm61bvk_20160729T200000Z', '');
update t set Hash = md5(id_google_calendar);
select * from t;
Result:
+-----------------------------------------------+----------------------------------+
| id_google_calendar | hash |
+-----------------------------------------------+----------------------------------+
| e5e3were760lkj792c7t5vm61bvk_20160729T200000Z | 06b5a13d9a7b0ed26ab1406434954972 |
+-----------------------------------------------+----------------------------------+
I think it would work.
Also I suggest that you test it first.
You can test query with:
Select statement:
SELECT id_google_calendar, MD5(id_google_calendar) as hash FROM ea_appointments
Update record in test table. Create some test table add few record in it and run query
I'm working with MySQL through phpMyAdmin. I need to reset the ID fields in one of the tables in my database, but I need to do it based on the publication date of each row. I've been looking everywhere and I can't seem to find a solution :(
The following lines of code work fine, but do not do exactly what I require based on the datetime column:
SET #count = 0;
UPDATE `table_name` SET `table_name`.`ID` = #count:= #count + 1;
So this is what I have:
+----+---------------------+
| ID | post_date |
+----+---------------------+
| 1 | 2013-11-04 20:06:28 |
| 2 | 2012-03-30 11:20:22 |
| 3 | 2014-06-26 22:59:51 |
+----+---------------------+
And this is what I need:
+----+---------------------+
| ID | post_date |
+----+---------------------+
| 1 | 2005-08-02 16:51:48 |
| 2 | 2005-08-02 16:59:36 |
| 3 | 2005-08-02 17:01:54 |
+----+---------------------+
Thanks in advance, guys :)
Try this, a simple approach though.
But you will lose all the relations to other tables since you are
resetting the PRIMARY ID Keys.
# Copy entire table to a temporary one based on the publication date
CREATE TEMPORARY TABLE IF NOT EXISTS `#temp_table` AS SELECT * FROM `wp_posts` ORDER BY `post_date`;
# Drop `ID` column from the temporary table
ALTER TABLE `#temp_table` DROP COLUMN `ID`;
# Reset the table `wp_posts` as well as its `ID`
TRUNCATE TABLE `wp_posts`;
# Exclude `ID` column in the INSERT statement below
INSERT INTO `wp_posts`(`post_author`, `post_date`, ..., `comment_count`) SELECT * FROM `#temp_table`;
# Remove the temporary table
DROP TABLE `#temp_table`;
Also see the ERD for WP3.0 below,
Ref: https://codex.wordpress.org/Database_Description/3.3
Try doing it with the following script. It selects every row of your table and orders the rows by its date ascending. Then your Update-Command will be executed within a loop.
Add the type of the ID of your table to the DECLARE-Statement and change the
field-Name in the UPDATE-Statement to your ID-Column name.
BEGIN
DECLARE col_id BIGINT;
DECLARE stepLoopDone BOOLEAN DEFAULT FALSE;
DECLARE counter INT DEFAULT 1;
DECLARE ORDER_CURSOR CURSOR FOR
SELECT id
FROM wp_posts
ORDER BY post_date ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET stepLoopDone = TRUE;
OPEN ORDER_CURSOR;
myLoop: LOOP
FETCH ORDER_CURSOR INTO col_id;
IF stepLoopDone THEN
LEAVE myLoop;
END IF;
/*YOUR UPDATE COMMAND*/
UPDATE wp_posts
SET id = counter
WHERE id = col_id;
/*YOUR UPDATE COMMAND*/
SET counter = counter + 1;
END LOOP;
CLOSE ORDER_CURSOR;
END
I want to update column value of any(provided) row value.
Table_A
+--------+-----------+
| num | text |
+--------+-----------+
| 1 | one |
| 2 | two |
| 3 | dont |
| 4 | four |
| 5 | five |
+--------+-----------+
I want to update 3rd row value. Something like this:
update Table_A set `text`='three' Limit 2,1
update Table_A set `text`='three' where 1 Limit 2,1
Assuming your "3rd row" has num column with value 3, you may try this:
UPDATE `Table_A` SET `text` = 'three' WHERE `num` = 3;
"I want to update 3rd row value" -- there is no "third row". The order in which rows are returned should be regarded as random (even if it usually isn't) unless you specifically add an ORDER clause.
Even then, you cannot refer to a row by its position; the query that effects the UPDATE has no way of knowing which row you're referring to from the previous SELECT. You would need some function referring to the row being selected; some SQL dialects have row_number() function, similar to old Clipper RECNO. See also here.
What you can do is choose some identifying value and use that to refer to the row(s) you want:
UPDATE `Table_A` SET `text` = 'three' WHERE `num` IN ( 3 );
UPDATE `Table_A` SET `text` = 'three' WHERE `text` IN ( 'dont' );
More information on updating a table based on a ranking (which I guess is what you're looking for) can be found on the manual page, see the notes about 'recno'.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE `Table_A` SET `text` = 'three' WHERE `num` = 3;
I think you expected like this
UPDATE Table_A t1 join (SELECT num,(#Row := #Row + 1) AS row_number
FROM Table_A, (SELECT #Row:=0) as row
) t2 ON t1.num=t2.num
SET t1.text='three' where t2.row_number='3'
I have this query:
mysql_query("
UPDATE users SET
`clicks_yesterday`=`clicks_today`,`clicks_today`=0)
My structure in my database looks like this:
My question is, how can I do so whenever I run the query above, clicks_yesterday get's the value of clicks_today?
Regards
That's how you do it. assignments in SQL are evaluated in the order encountered (e.g. left -> right). But if you want to be ENTIRELY sure that things are assigned properly, then split it into two queries:
UPDATE users SET clicks_yesterday = clicks_today;
UPDATE users SET clicks_today = 0;
For MySQL what you have written works. It is however different on different DBs so it's important to test.
These are my tests for this question
CREATE TABLE `duals` (
`one` int(11) DEFAULT NULL,
`two` int(11) DEFAULT NULL
);
insert into duals values (1, 2);
select * from duals;
+------+------+
| one | two |
+------+------+
| 1 | 2 |
+------+------+
update duals set one = two, two = 0;
select * from duals;
+------+------+
| one | two |
+------+------+
| 2 | 0 |
+------+------+