Basically, I want to update a column of numeric value, setting it to its previous value multiplied by a constant, but for an entire table worth of rows that meet the criteria, any help would be appreciated
edit: also every row has values independent from each other
edit 2: I'm looking for something like UPDATE table SET rowsColumnValue TO rowsColumnValue * constant WHERE differentColumnusedAsIdentifier = someNumber but that doesn't set every row to the same number, but to its previous value in that column multiplied by the given constant
edit 3: basically I want that if in a row a column value is 2 and in another one is 4, now they become 4 and 8
Please try this and let me know if it helps:
Update table_name set column_name = column_name * constant
[where column = condition]
Related
I have an auto increment column. I want to change value of another column based on this auto increment column value.
another column value ==> (auto increment column value/3) +1
How do I do it via query? Is it possible?
I want to do it for newly creating rows.
Is the new (id/3)+1) value ever going to change?
Judging from the question, the data only exists at the time of the insert. The auto increment column will never change as it has to be a Primary Key, I would only insert on it if you have to join on it or if it is going to change in future. You can get this derived value from a SELECT query after your insert.
SELECT id, ((id/3)+1) as derived value from table;
It is a "derived" value and you should consider not to store this value but calculate it in your PHP code every time you need it. Here is a post where you can find further explanations about when to store derived values: Storing “derived” values vs calculating them on extraction
#Octopi solution could solve your problem.
Try the following:
INSERT INTO your_table (your_column)
SELECT (MAX(auto_increment_column) / 3) + 2 FROM your_table;
EDIT
If you want to calculate the column depending on the number of values use this:
INSERT INTO your_table (your_column)
SELECT (COUNT(*) / 3) + 2 FROM your_table;
EDIT 2
You can accomplish the same using:
INSERT INTO your_table (your_column) VALUES (
(SELECT (MAX(aa.auto_increment_column) / 3) + 2 FROM your_table AS aa)
);
EDIT 3
For more columns you can use something like this:
INSERT INTO your_table (your_column, some_other_column) VALUES (
(SELECT (MAX(aa.auto_increment_column) / 3) + 2 FROM your_table AS aa), some_other_column
);
You can find more info here:
Select from same table as an Insert or Update
In fact you have to add +2 because it's 1 from you rule and +1 to increment the MAX(id).
I want to add data to a table, one column at a time. Therefore I don't want to always add a new row, as the column I'm adding to may not have much data in compared to the other columns. As a result I suppose I want to add the data to the first empty cell in the specified column without necessarily adding a new row to the whole table (as there may already be enough rows).
My table has 5 columns.
I'm currently using:
$sql=("INSERT INTO [MY_TABLE] ([MY_COLUMN]) VALUES ('[MY_DATA]')");
Please could someone assist with the correct code? I know it's basic stuff but I'm new to mySQL.
Thanks in advance.
So you can use UPDATE statement with a WHERE condition to target that specific row
UPDATE table_name SET column_name = 'whatever' WHERE column_name = '' AND id = '1'
If you want to update all rows where that column is blank than simply use
UPDATE table_name SET column_name = 'whatever' WHERE column_name = ''
no need of using id in above query
Reference on UPDATE
I have 2 tables: user_values and decrease_times.
user_values has 3 columns: username, property, value and decrease_times has 3 columns: property, decrease_value, decrease_time. All of these are dynamic and change a lot.
I have a cron job that gets called every hour, and what I want to do is reduce all the value rows from user_values with the amount stored in decrease_value if property from user_values is equal to property from decrease_times.
Also, decrease_time can be 0 or 1, 0 means it should decrease every 1 hour, and 1 means it should decrease every 24 hours. I already implemented an if-clause that detects if it is 6am, and then it should decrease all the values if they are 1 or 0.
What query should I make to do this? Also, this is done in PHP.
Edit: What I did so far is this: UPDATE user_values SET value = value - (SELECT decrease_value FROM decrease_times WHERE property=/* property from UPDATE clase should be here */) WHERE property=(SELECT property FROM decrease_times WHERE decrease_time=0)
You don't need to use a select statement anywhere...
UPDATE user_values AS values
INNER JOIN decrease_times AS times ON values.property = times.property
SET values.value = values.value-times.decrease_value
WHERE values.property = times.property AND times.decrease_time = 0
You are taking your table you're looking to edit, and joining it for the query with the second table... You set the value field on your user_values table using the simple maths equation you need, and ensure that both the property values are the same, and that you're only updating the rows that are meant to be updated every hour.
Your daily version would be the same, but wouldn't need "AND..." onwards
something like this UPDATE TABLE SET your_value=(your_value-1) WHERE USERID=5
or
UPDATE TABLE1 SET your_value=(your_value +(SELECT your_value2+5 FROM TABLE2 WHERE USERID=5)) WHERE USERID=5
-- adapt it to your needs; this is basic SQL syntax.
Need better example and your table structure -- you can use http://sqlfiddle.com/ and recreate some of your tables/data so we can play with it
http://www.w3schools.com/sql/sql_update.asp
I have a table with an value that I would like to be updated periodically with a cron job. However, I need to update the value by replacing it with a value from a different table. The issue is that I would like the replacement value to be chosen randomly.
For example, Table 1 has
ID Email
=================
1 bobatumail
Table 2 has:
ID Email
================
1 bobatumail
2 joeatumail
3 peteatumail
4 biffatumail
5 wilneratumail
6 wilsonatumail
I would like the query to replace bobatumail in Table 1 with any of the other values in Table 2 as long as it is random. It could even be the same value as in Table 1.
Any idea how to do this?
In MySQL you could use the REPLACE statement:
REPLACE INTO table1 (ID, Email)
SELECT 1, table2.Email FROM table2 ORDER BY RAND() LIMIT 1;
The "1" in the second line represents the id of the entry while the second part returns a random value out of table2. Yes, there are solutions using the UPDATE statement (JOIN and ANSI) but its always tricky and you usually have to turn off safe update mode.
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_safe-updates
Please note that REPLACE first deletes the old entry and then reinserts the new one.
http://dev.mysql.com/doc/refman/5.5/en/replace.html
I need to get next id from table (auto_increment).
I could just use SELECT * from table ORDER BY id DESC LIMIT 1;
For example I get 50. But if we delete from table two items I will get 48 but correct one
will be 51. How get correct value even we something delete from table ?
You can only use SHOW TABLE STATUS LIKE 'tablename' to fetch the auto_increment value. A simpler solution might be: SELECT MAX(id) + 1 FROM table, but this is buggy if the last entry was deleted.
show table status like 'table_name'
next id value is in 'Auto_increment' field
SHOW TABLE STATUS LIKE 'table'
The value you want is in the Auto_increment field.
Be careful about concurrency though: by the time you get around to using this value, some other client could have inserted into the table and thus your value is out of date. It's usually best to try to not need this.
SELECT LAST_INSERT_ID() + 1;
gets the last ID used in an insert in an autoincrement column + 1
I see two solutions for the next ID:
1) Select bigger value of a column with max function. Example: select max( id ) from table;
2) Using the command SHOW STATUS LIKE and get the correct index of array. Take a look: http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
Seems to me you're creating a race condition here.
Why exactly can you not insert the row you want to insert and then use LAST_INSERT_ID() to find it's ID?