how to properly do the following update MYSQL statement? - mysql

i have a string with the following value
Germany,Sweeden,UAE
and i have an mysql row that have a value of one of this separated string with comma
as example a Country Row with a value Sweeden .
i have tried to use this sql statement to update a targeted row where its value is like one of this string separated with comma
hence if the row have a value of sweeden it should be updated sense sweeden value is include in the comma string
UPDATE CountryList SET Time= '100' WHERE Country LIKE '%Germany,Sweeden,UAE%'
but i couldn't update the targeted row using this syntax. any idea what is the correct syntax i should use ?

I think you want find_in_set():
update CountryList
set time = 100
where find_in_set(country, 'Germany,Sweeden,UAE')
This checks if country matches any of the values in the comma-separated row given as second argument to find_in_set().
Note that I removed the single quotes around the literal 100: if time is a number, then treat it as such (if it's a string instead, then keep the quotes).

Related

Trim leading and update in mysql

I wanted to create a query in MYSQL that would trim the leading '# ' from all of the fields in a column.
update values set value = TRIM(LEADING '# ' from value)
However, doing this gives me an error
Duplicate entry '3002' for key value
The value column has a unique constraint and the error probably occurs because the query is trying to set the same value to all of value column after trimming.
Is there a way to do trim leading and update in mysql?
This query looks fine.
The issue might be here. Let me give you an example.
Case 1:
At row-x, you have value '#info' and at row-y you have info.
You removed the # from row-x in your query. Now you have already info value at row-y. You can not update the new value to info as it is there already.
I would suggest either to remove the UNIQUE constraint or you do not update the database itself. You can trim with your backend programming.
This is not setting all values to the same value, this is where once trimmed you now have two different rows with the same value for that column.
To find this:
SELECT id, value, TRIM(LEADING '# ' from value) AS trimmed_value ORDER BY trimmed_value
Presuming you have some kind of id column you'll be able to find any rows where trimmed_value is identical.

Obtain result set from table within column range?

How to obtain ResultSet from DB table within range ?
String sql=SELECT * From Employee Where Salary<=? AND Salary>=?;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
String lowest_salary=In.nextLine();
String max_salary=In.nextLine();
preparedStatement.setString(1,lowest_salary);
preparedStatement.setString(1,max_salary);
ResultSet resultSet = preparedStatement.executeQuery();
Can I assigned values as parameter twice to DB table column(EmployeeSalary)? What's solution?
You are close. Your main issue is that you are using .setString(1, ...) twice, which is incorrect. You have two parameter placeholders (?) in your SQL command text so you need to set the first parameter value with the index 1 and the second parameter value with the index 2.
Also be sure that you set the parameter values using the method that matches the column type. .setString is to be used with text columns. A column named "Salary" is more likely to be numeric, so you should probably be using .setInt if the column is integer, or .setBigDecimal if the column is a decimal or currency type.

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.

How to remove the first characters in a column

I have a column that contains string. It is a number but saved as string. The string might start with 00 or more than two zeros. I need to remove the zeros and insert the new value (after removing) into another column. The problem is the number of zeros at the beginning is not fixed. It can be 2 or more. Is this task possible to be done with MySQL ? How?
A good hint is provided here:
UPDATE your_table
SET column2 = TRIM(LEADING '0' FROM column1)
supposing that the original value is stored in column1 and you want to write the 0-trimmed one in column2
you can use CONVERT(col,UNSIGNED INTEGER) to convert it into the number, that should remove the leading zeros.
query can be
insert into <table> values(select CONVERT(col,UNSIGNED INTEGER),..... from <table2>);

mysql syntax for populating a column of an existing table and setting a default value for said column

I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?