Update column with concat of second column and a variable - php

I need to update column values (say column2) with concatenation of two string variables (say str1 & str2) with another column (say column1). Final value which I need in column2 is str1 as prefix and str2 as suffix to column1 value.
One way which can be done is with a dummy third column and a total of 4 update queries.
update table set column3 = str1;
update table set column2 = concat(column3, column1);
update table set column3 = str2;
update table set column2 = concat(column2, column3);
But I want to reduce that to a single update without using a dummy column, like below -
update table set column2 = concat($str1, column1, $str2);
I need help in the concat part of the above query.

There's no reason this not to work:
$prefix = mysql_realescape_string($str1);
$suffix = mysql_realescape_string($str2);
$sql = "UPDATE table SET cloumn2 = CONCAT('$prefix', column1, '$suffix')";

Assuming you are using PHP, there is your solution:
$query="update table set column2 = concat('$str1', column1, '$str2');";
You were actually very close to solution.

Try something like:
$sql = "UPDATE table SET column2 = CONCAT_WS('-', '$str1', column1, '$str2');";
The first argument for CONCAT_WS is the string character you'd like to concatenate with.

Related

UPDATE if ID already exists? [duplicate]

I've seen the following (using the VALUES option):
$query = "INSERT INTO $table (column-1, column-2, column-3) VALUES ('value-1', 'value-2', 'value-3') ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2, column3 = value3, ID=LAST_INSERT_ID(ID)";
... but I can't figure how to add ON DUPLICATE KEY UPDATE to what I'm using:
$query = "INSERT INTO $table SET
column-1 ='value-1',
column-2 ='value-2',
column-3 ='value-3'
";
e.g.:, pseudo-code
$query = "INSERT INTO $table SET
column-1 ='value-1',
column-2 ='value-2',
column-3 ='value-3'
ON DUPLICATE KEY UPDATE SET
column1 = value1,
column2 = value2,
column3 = value3,
$id=LAST_INSERT_ID(id)";
$my_id = mysql_insert_id();
";
I would find the latter easier to read. Would appreciate clarification, didn't find an example in the manual.
cheers
I've used ON DUPLICATE KEY UPDATE a lot. For some situations it's non-standard SQL extension that's really worth using.
First, you need to make sure you have a unique key constraint in place. The ON DUPLICATE KEY UPDATE function only kicks in if there would've been a unique key violation.
Here's a commonly used format:
$query = "INSERT INTO $table (column1, column2, column3)
VALUES ('value-1', 'value-2', 'value-3')
ON DUPLICATE KEY UPDATE
column1 = values(column1),
column2 = values(column2),
column3 = values(column3);"
column1 = values(column1) means "Update column1 with the value that would have been inserted if the query hadn't hit the duplicate key violation." In other words, it just means update column1 to what it would've been had the insert worked.
Looking at this code, it doesn't seem correct that you're updating all three of the columns you're trying to insert. Which of the columns has a unique constraint on it?
EDIT: Modify based on 'SET' format of mysql insert statement per the question from the OP.
Basically to use ON DUPLICATE KEY UPDATE, you just write the insert statement as you normally would, but add the ON DUPLICATE KEY UPDATE clause tacked onto the end. I believe it should work like this:
INSERT INTO $table
set column1 = 'value-1',
column2 = 'value-2',
column3 = 'value-3'
ON DUPLICATE KEY UPDATE
column1 = values(column1),
column2 = values(column2),
column3 = values(column3);
Again, one of the columns you're inserting has to have a unique index (or a combination of the columns). That can be because one of them is the primary key or because there is a unique index on the table.
Have you tried using REPLACE INTO instead?
http://www.mysqlperformanceblog.com/2007/01/18/insert-on-duplicate-key-update-and-replace-into/

Inserting a value into all rows in the column in php

I wanted to overwrite all the values in the Project1 column with 0. Is there a way to do this ? Like all the students in studentid will all have 0 values in Project1.
You have to use an update query.
For your example:
UPDATE `TABLE_NAME` SET `Project1` = 0
DO :
UPDATE table_name SET Project1=0
Just omit where clause in Mysql, run this
update `mytable` set
`Project1` = 0
;

Update a whole column instead of a row

Ok is there a possibility to update a column instead of a row?
f.e something like that:
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids', $_POST['input1']);
$laninpstmt->bindParam(':allids', $_POST['input2']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
If i explain the code it's like:
Update all the rows(allids) from one column in a table
Running your query without a where clause will update all rows, and if you update a single field it will be the same as updating a column
UPDATE `test` SET `field_5` = 7
Will update table test and set all values in the column field_5 to 7
You could use IN:
Apparently, you need to do your own query, see https://stackoverflow.com/a/920523/2575355.
$inputs = array($_POST['input0'], $_POST['input1'], $_POST['input2']);
$allids = implode(', ', $inputs)
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id IN ($allids)");
You forgot to specify the value for you column_name like that
UPDATE table SET column_name = 'Some_value here' WHERE id = :allids
i guess you want do this
$laninpstmt = $db->prepare ("UPDATE table SET column_name = Concat(:allids1 , :allids2, :allids3, :allids4) WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids1', $_POST['input1']);
$laninpstmt->bindParam(':allids2', $_POST['input2']);
$laninpstmt->bindParam(':allids3', $_POST['input3']);
$laninpstmt->bindParam(':allids4', $_POST['input4']);

Can I use ON DUPLICATE KEY UPDATE with an INSERT query using the SET option?

I've seen the following (using the VALUES option):
$query = "INSERT INTO $table (column-1, column-2, column-3) VALUES ('value-1', 'value-2', 'value-3') ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2, column3 = value3, ID=LAST_INSERT_ID(ID)";
... but I can't figure how to add ON DUPLICATE KEY UPDATE to what I'm using:
$query = "INSERT INTO $table SET
column-1 ='value-1',
column-2 ='value-2',
column-3 ='value-3'
";
e.g.:, pseudo-code
$query = "INSERT INTO $table SET
column-1 ='value-1',
column-2 ='value-2',
column-3 ='value-3'
ON DUPLICATE KEY UPDATE SET
column1 = value1,
column2 = value2,
column3 = value3,
$id=LAST_INSERT_ID(id)";
$my_id = mysql_insert_id();
";
I would find the latter easier to read. Would appreciate clarification, didn't find an example in the manual.
cheers
I've used ON DUPLICATE KEY UPDATE a lot. For some situations it's non-standard SQL extension that's really worth using.
First, you need to make sure you have a unique key constraint in place. The ON DUPLICATE KEY UPDATE function only kicks in if there would've been a unique key violation.
Here's a commonly used format:
$query = "INSERT INTO $table (column1, column2, column3)
VALUES ('value-1', 'value-2', 'value-3')
ON DUPLICATE KEY UPDATE
column1 = values(column1),
column2 = values(column2),
column3 = values(column3);"
column1 = values(column1) means "Update column1 with the value that would have been inserted if the query hadn't hit the duplicate key violation." In other words, it just means update column1 to what it would've been had the insert worked.
Looking at this code, it doesn't seem correct that you're updating all three of the columns you're trying to insert. Which of the columns has a unique constraint on it?
EDIT: Modify based on 'SET' format of mysql insert statement per the question from the OP.
Basically to use ON DUPLICATE KEY UPDATE, you just write the insert statement as you normally would, but add the ON DUPLICATE KEY UPDATE clause tacked onto the end. I believe it should work like this:
INSERT INTO $table
set column1 = 'value-1',
column2 = 'value-2',
column3 = 'value-3'
ON DUPLICATE KEY UPDATE
column1 = values(column1),
column2 = values(column2),
column3 = values(column3);
Again, one of the columns you're inserting has to have a unique index (or a combination of the columns). That can be because one of them is the primary key or because there is a unique index on the table.
Have you tried using REPLACE INTO instead?
http://www.mysqlperformanceblog.com/2007/01/18/insert-on-duplicate-key-update-and-replace-into/

MySQL: Updating all rows setting a field to 0, but setting one row's field to 1

Is there an efficient way to update a selection of rows' field to 0, but set One of these rows to 1 based on an ID.
Basically, I have multiple objects in a database, and I want to toggle between which one is "inuse", so the query will set one of the rows (by id) to inuse=1 and the others to inuse=0.
Thanks :)
UPDATE `table`
SET `inuse` = (`id` = 23)
Sure
UPDATE table SET inuse=IF(id=ABCD, 1, 0)
would set the inuse field to 1 if id is ABCD and 0 otherwise.
UPDATE table
SET inuse = (id = #id)
WHERE id = #id
OR inuse
This will update only relevant rows.
UPDATE myTable
SET Field = 0
WHERE FieldID <> [WhateverID]
UPDATE myTable
SET Field = 1
WHERE FieldId = [WhateverID]
Try
update tbl set inuse = if(test, 1, 0);
or shorter
update tbl set inuse = test;
for example
update tbl set inuse = name = 'foo';
If you are after to set a flag, so no other part of the code uses the same object at the same time, it's better if the calling code sets inuse=1 and resets it when done. Otherwise you will end up one thread to mark an object (row) as inuse, and then if another thread needs another object, it will reset the first one, while still in use.
If this is not the case, and you just want be able to set inuse for one, and reset all others, you can use:
UPDATE myTable
SET InUse = CASE
WHEN myTable.id = #id THEN 1
ELSE 0
END
if your database uses transactions, this is the best way to do it:
update myTable set inuse = 0;
update myTable set inuse = 1 where id = ?;
if you are not using transactions, then the other answer using CASE is the best option since it's portable. but it will require more CPU time than the two UPDATE statements.

Categories