How do I modify/append a table row using MYSQL? - mysql

What I know is that you can append values from a query using concat function in MYSQL.
>col1 = ben
UPDATE table set col1 = concat(col1, ' altered')
>col1 = ben altered
But what I am trying to achieve is to append anywhere on the col.
col1 = ben
UPDATE table set col1 = "altered " + col1
-- pseudo code
col1 = altered ben
Is there a function for this in mysql?

If you want add before the column simply change the concat sequence
UPDATE table set col1 = concat('altered ' , col1 )
You can se concat with all the column and value you prefer
concat('altered ' , col1, ' altered')
concat('altered ' , col1, ' altered', col2, col3, 'altered again')

Related

Using And & OR condition in MySQl With Same Query

How to use the And, OR condition in the same MySQL where query.
SELECT * FROM Table_name
WHERE filed_name = 0 AND (field1 != '' AND field2 ='')
OR (field1 = '' AND field2 != '') AND filed3 = 1;
I want to more than 2 fileds in brackets.
You can include as many conditions as you need within parentheses, but you do need to be careful with OR and you may find it necessary to combine multiple sets of conditions with parentheses. When this happens consistent formatting and use of indents can help you maintain the required logic.
SELECT *
FROM Table_name
WHERE (field_name = 0
AND (field1 != '' AND field2 ='')
)
OR (field3 = 1
AND (field1 = '' AND field2 != '')
)
;
Do note that the query above is a guess; I have assumed you need 2 sets of conditions.
SQL refer to columns (not at fields)
anyway you can use all the columns you need in each where condition
SELECT *
FROM Table_name
WHERE col0 = 0
AND col1 != ''
AND col2 =''
OR (col1 = '' AND col2 != '' AND col3 ='YOUR_VALUE')
AND col3 = 1
OR (col4 = 'your_value4' AND col5> 100 );
the parentheses are needed for change the priority in evalaution of the condition ...

UPDATE multiple table with same column name with condition for each value on MYSQL

I've a column "pid" and a lot of table on each database , then I want to update all table from every database with column "pid" and set 'pid' = 5 where 'pid' = 3 and set 'pid' = 6 where 'pid = '7' on 1 query .
I've find post like this and try to apply it:
Select 'UPDATE ' + TABLE_NAME + ' SET pid = ''5'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'pid'
Without condition for try it , then MYSQL just return me a select with value 0 .
'UPDATE ' + TABLE_NAME + ' SET pid = ''5'' '
0
Need a little help and explain for make this query work and understand how its work .
Really thanks everyone :)
UPDATE table_name
SET pid = 5
WHERE pid = 3;
It should be implemented like this. You simply tell to update the table named table_name to set pid as '5' wherever it finds it as '3'.
Read also here. ;)

UPDATE specified multiple tables with the same column name

I have multiple tables with matching column names and want to make a update statement affecting 1 column in 4 specific tables like this:
UPDATE table1, table2, table3, table4
SET table1.column_1 = 'value', table2.column_1 = 'value', table3.column_1 = 'value', table4.column_1 = 'value'
WHERE table1.column_id = 'value' OR table2.column_id = 'value' OR table3.column_id = 'value' OR table4.column_id = 'value'
This is not working, and i googled many hours for an answer, and ive found this https://stackoverflow.com/a/19797529/1824324:
Select 'UPDATE ' + TABLE_NAME + ' SET CreatedDateTime = ''<<New Value>>'' '
From INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'CreatedDateTime'
And with a little tweaking:
SELECT 'UPDATE ' + TABLE_NAME + ' SET column_1= ''Value'' where column_id=''value'''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'column_1'
Now my problem is i don't want every table with that particular column to be updated, only from table1, table2, table3, and table4. How do i cut the "FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'column_1'" and replace with the 4 tables?
I believe you are talking about MySQL due to the tables you are using.
Simply filter the TABLE_NAME column:
SELECT 'UPDATE ' + TABLE_NAME + ' SET column_1= ''Value'' where column_id=''value'''
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'column_1'
AND TABLE_NAME IN ('table1', 'table2')

Truncated incorrect DOUBLE value error on SQL statement?

I'm trying to append a string to an existing record in a MySQL Database:
UPDATE `db`.`tbl` SET field1 = IFNULL(field1, '') + ',' + '12/15/16: $50' WHERE field2 = 'xyz'
In MySQL, + is exactly what it suggests: addition. You are getting an error on arithmetic presumably because the strings are not converted to a number (fortunately -- otherwise you would silently get the wrong answer).
So, try this:
UPDATE `db`.`tbl`
SET field1 = CONCAT(COALESCE(field1, ''), ',', '12/15/16: $50')
WHERE field2 = 'xyz';
Or, if you don't want the comma if field1 is NULL:
UPDATE `db`.`tbl`
SET field1 = CONCAT(COALESCE(CONCAT(field1, ','), ''), '12/15/16: $50')
WHERE field2 = 'xyz';
to append in MySQL, use concat not + eg
select concat('something', 'something_else');
returns
somethingsomething_else
you can also use concat_ws (with separator)
select concat_ws('#','something', 'something_else')
returns
something#something_else

Update multiple rows in a single MySQL query

I am trying to run this:
UPDATE test
SET col2=1 WHERE col1='test1',
SET col2=3 WHERE col1='test2';
The error I am getting:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
My table:
CREATE TABLE `test` (
`col1` varchar(30) NOT NULL,
`col2` int(5) DEFAULT NULL,
PRIMARY KEY (`col1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
It’s something about , at the end of the first row. When I changed it to ;, it didn’t recognize col2. How can I do this in one query?
This is most clear way
UPDATE test
SET col2 = CASE col1
WHEN 'test1' THEN 1
WHEN 'test2' THEN 3
WHEN 'test3' THEN 5
END,
colx = CASE col1
WHEN 'test1' THEN 'xx'
WHEN 'test2' THEN 'yy'
WHEN 'test3' THEN 'zz'
END
WHERE col1 IN ('test1','test2','test3')
Consider using INSERT-ODKU (ON DUPLICATE KEY UPDATE), because that supports to update multiple rows.
Make sure that the values of all PK columns are in the VALUES().
Where feasible, generate the SQL with data from a slave.
you can use CASE on this
UPDATE test
SET col2 = CASE WHEN col1 = 'test1' THEN 1 ELSE 3 END
WHERE col1 IN ('test1', 'test2')
or IF (for MySQL only)
UPDATE test
SET col2 = IF(col1 = 'test1', 1, 3)
WHERE col1 IN ('test1', 'test2')
alternatively when the construct with cases gets too unreadable, you could/should start a transaction and just do the updates sequentially.
this usually results in more straightforward sql, except if the first statements creates rows that then are matched by the second statement when they should not. however this is not the case in your example.
This is how I did it:
UPDATE col1 (static value), col2 (static value), and col3 (different values) WHERE col4 has different values AND col5 is static.
$someArray = ["a","b","c"];
$anotherArray = [1,2,3];
$sql = "UPDATE table SET col1 = '$staticValue1', col2 = '$staticValue2', col3 = CASE col4";
$sqlEnd = " END WHERE col4 IN (";
$seperator = ",";
for ( $c = 0; $c < count($someArray); $c++ ) {
$sql .= " WHEN " . "'" . $someArray[$c] . "'" . " THEN " . $anotherArray[$c];
if ( $c === count($someArray) - 1 ) {
$separator = ") AND col5 = '$staticValue5'";
}
$sqlEnd .= "'" . $someArray[$c] . "'" . $seperator;
}
$sql .= $sqlEnd;
$retval = mysqli_query( $conn, $sql);
if(! $retval ) {
/* handle error here */
}
And the output string for MySql query would be something like this:
UPDATE table SET col1 = '1', col2 = '2', col3 = CASE col4 WHEN 'a' THEN 1 WHEN 'b' THEN 2 WHEN 'c' THEN 3 END WHERE col4 IN ('a','b','c') AND col5 = 'col5'
With MySQL8 it can be done smarter,
but also consider creating a temporary result set, inside the SQL,
to JOIN with:
UPDATE MyTable t JOIN (
SELECT 'v1a' AS c1, 'v1b' AS c2
UNION ALL SELECT 'v2a', 'v2b'
UNION ALL SELECT 'v3a', 'v3b'
) AS u USING (c1)
SET t.c2 = u.c2