How to update column if another column fulfill specific criteria? - mysql

I need to update one column if another column has a specific data.
Usually if I want to update one column, I do the following SQL Query:
UPDATE table1
SET field1 = replace(field1, 'oldstring', 'newstring')
But what I can't figure is how to make it look up one column, and if that field has some data, it should update another column's field.
Here is what I want to do.
look in table: phpbb_tree
under column: spouses_total
if the field is empty (has no data)
update column: page_template
update from: tree_body_spouse_1.html to: tree_body_single.html
So basically, I know how to do the "update" part, but don't know how to make it look first in one column, and if empty (or matches) it should do the following:
UPDATE phpbb_tree
SET page_template = replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
Hopefully someone could tell me how to write it up. I don't even know if it's even possible to do a search for an empty data in a column?

You could use CASE expression to fulfill different condition of replacement.
UPDATE phpbb_tree
SET page_template = (CASE
WHEN spouses_total is null
THEN replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
ELSE page_template
END
);
Edit:
Please check this..
SQL Fiddle HERE

Related

UPDATE field value if contains at least this text

I am trying to find the correct SQL query to UPDATE fields from a table that contain at least some text. My idea is something like this.
UPDATE this IN tablename WHERE fieldname = "%value%"
I know something is wrong but I am new to this and in need of some help. Thanks
INSERT statement will add new row(s) into your table.
The UPDATE statement will edit your current row(s).
You should at least shout if you want some help.
Please post something more for your question, like table name, column names and your exact requirement.
UPDATE statement is something like this:
UPDATE tablename SET field1 = 'TEST' WHERE fieldname LIKE "%value%";

Trying to use update query to only update fields that are blank in Microsoft Access

I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.

MYSQL: Update field with concat of multiple fields

I'm trying to update a field of my table with the CONCAT of the some fields of the same table.
Whith this
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
This query has 0 rows affected and no errors.
With this other query
UPDATE tabex SET field1=CONCAT_WS(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
If the content of some of a(n) fields is NULL mysql puts a copy of the previous result
Someone can help me?
When this query
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
doesn't affect a row, the only explanation would be, that the table is empty. It would update every row in the table. But if one of the columns is NULL, your field1 column will also be NULL.
To avoid that, you have to use the COALESCE() function. This function returns the first of its parameters which is not NULL.
UPDATE tabex SET field1=CONCAT(COALESCE(tabex.a1, ''),', ',...);
On a sidenote I have to ask, why you want to do this. Comma separated values in columns are a bad idea most of the times.
And finally, your query using CONCAT_WS() is wrong. The _WS in the function name is short for "with separator", so the first parameter is the separator which then is placed between the other parameters of the function. So you should write it like this:
UPDATE tabex SET field1=CONCAT_WS(',', tabex.a1, tabex.a2, tabex.a3,...);
Another advantage of the CONCAT_WS() function is, that it ignores NULL values. Read more about the two functions in the manual.

MySQL how to insert one value without affecting other values

In my table "accounts" I have four columns like
user, pass, column1, column2
I need to insert value into column2, where user='special_user_value'.
How can I do this?
UPDATE accounts
SET column2 = 'New Value'
WHERE user = 'special_user_value';
You don't "insert values" into a column. You insert a row, that has a value for all the columns you specified in the table creation; Just like a real table, or excel sheet for that matter.
If you need to change a column value for a specific row, you can use UPDATE:
UPDATE table_name SET column2='new value' WHERE user='special_user_value'
This is a really basic example. If you follow the link I provided for UPDATE, you may learn more about changing table values for a specific row.
If are you looking for actually inserting a new row with a specific value for that column, there's INSERT INTO you could follow to achieve that.
Use update Query like:
Update table_nm set field1=value1, Field2=value2 Where condition;
UPDATE Accounts SET column2='NewValue' WHERE user='special_user_value'

REPLACE statement with embedded IF() logic?

EDIT: This actually works fine, no idea why I thought otherwise.
I have a prices table which includes a column price_was which needs to contain the highest ever value for prices.
Is it possible to do a REPLACE query which would update this if required?
The following (which is simplified and built dynamically in PHP) doesn't seem to work.
REPLACE prices
SET price = 1.99,
price_was = IF(1.99 > price_was, 1.99, price_was)
id_product = 1
I'm thinking perhaps it's not possible, but would love to hear otherwise since I'm updating many records and need to be as efficient as possible.
The query you posted is indeed valid, try it for yourself. I would use an UPDATE though since you're only updating one field and the REPLACE can possible over-write other column data you want left alone.
Try INSERT ... ON DUPLICATE KEY UPDATE instead:
INSERT INTO prices (price, price_was, id_product)
VALUES (1.99, 1.99, 1)
ON DUPLICATE KEY UPDATE
price_was = IF(VALUES(price) > price_was, VALUES(price), price_was)
id_product = VALUES(id_product)
This will do either an INSERT or an UPDATE, while the REPLACE statement does either an INSERT or a DELETE followed by an INSERT. You are not able to reference old values in a REPLACE statement, probably because of the DELETE/INSERT semantics. From the docs:
Values for all columns are taken from
the values specified in the REPLACE
statement. Any missing columns are set
to their default values, just as
happens for INSERT. You cannot refer
to values from the current row and use
them in the new row. If you use an
assignment such as SET col_name =
col_name + 1, the reference to the
column name on the right hand side is
treated as DEFAULT(col_name), so the
assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.