MySQL query to concat prefix to existing value in a field - mysql

My table structure is as follows,
ID Name Source
1 John first.jpg
2 Doe second.jpg
3 Mary third.jpg
4 Kurian four.jpg
I would like to update the "Source" by prepending with the host and primary key as follows
http://example.com/1/first.jpg
http://example.com/2/second.jpg
http://example.com/3/third.jpg
http://example.com/4/four.jpg
tried with CONCAT("http://example.com/"+id,Source) but fails with Truncated incorrect DOUBLE value:
Any suggestion will be greatly apreciated.

Try
UPDATE table_name
SET Source = CONCAT('http://example.com/', ID, '/', Source);
Result
| ID | Name | Source |
|----|--------|---------------------------------|
| 1 | john | http://example.com/1/first.jpg |
| 2 | Doe | http://example.com/2/second.jpg |
| 3 | Mary | http://example.com/3/third.jpg |
| 4 | Kurian | http://example.com/4/fourth.jpg |

checkout this
SELECT CONCAT("http://example.com/" , CONCAT(ID , CONCAT("/" , Source))) FROM table_name;
or simply
SELECT CONCAT("http://example.com/" ,ID , "/" , Source) FROM table_name;

You can iterate in sql but the easiest way is to create a php script in which you create an array with the rows of the table
Then use the dot si tax to concat ID with the domain name and the source, something like:
$newval = ‘{$table[“ID”]}’ .“/Domainname.com/” . ’{$table[“Source”]}’;
Where $table is a variable storing the associative array of the table
And then make a new query in which you override the source column for each row in the table array

Please try below code:
UPDATE table_Name
SET Source = concat(concat(CONCAT('http://example.com/',ID),'/'),source);

Related

Looping through an array in SQL column

I have a SQL table that looks something like this:
| ID | Value |
| --- | ----------------------------------------------------- |
| 1 | {"name":"joe", "lastname":"doe", "age":"34"} |
| 2 | {"name":"jane", "lastname":"doe", "age":"29"} |
| 3 | {"name":"michael", "lastname":"dumplings", "age":"40"}|
How can I using SQL select function, select only the rows where "age" (in value column) is above 30?
Thank you.
The column Value as it is it contains valid JSON data.
You can use the function JSON_EXTRACT() to get the the age and convert it to a numeric value by adding 0:
SELECT *
FROM tablename
WHERE JSON_EXTRACT(Value, "$.age") + 0 > 30;
See the demo.

How do I remove .0 from a db table column?

I converted an Excel file into a SQL file. But, when I imported it into my database. It added .0 at the end of some values. The column type is varchar. How can I update the column values to remove .0 from all the string values?
For example:
Imported Values
12345.0
A119B
65489.0
BD585
123.0
124.0
12.0
Desired Result
12345
A119B
65489
BD585
123
124
12
If the decimal value is not certain, you should consider using SUBSTRING_INDEX() ; either in SELECT or UPDATE. Now you're just showing example of .0 but what if it has more variation than just .0? I assume at least that the unwanted value is always going to be after . so:
SELECT SUBSTRING_INDEX(val,'.',1) FROM mytable;
result:
+----------------------------+
| SUBSTRING_INDEX(val,'.',1) |
+----------------------------+
| 12345 |
| A119B |
| 65489 |
| BD585 |
| 123 |
| 124 |
| 12 |
+----------------------------+
Use same function for the update:
UPDATE mytable
SET val=SUBSTRING_INDEX(val,'.',1)
WHERE val LIKE '%.%';
Demo fiddle
UPDATE table SET colName = REPLACE(colName, ".0", "")
Credit goes to: #WOUNDEDStevenJones
Note: change table with your table name and colName with your column Name.

Select rows with non english text

I have table with this structure :
IdCity int
CityName nvarchar(20)
the data inside it is written in russian like :
+--------+----------------+
| IdCity | CityName |
+--------+----------------+
| 1 | Абакан |
| 2 | Азов |
| 3 | Александров |
| 4 | Алексин |
+--------+----------------+
I tried to make an easy view to retrieve the rows that have city name = Азов :
SELECT IdCity, CityName
FROM dbo.City
WHERE (CityName = 'Азов')
It gave me null although the record is in the table.
When I tried to add a row with an english name:cityname = abc, for example and edited the view to select cityname = 'abc', it worked fine.
so how to make the sql query select the russian inputs also?
It can be the encoding issue. make sure your CityName type is VARCHAR, not NVARCHAR. and try to run this script.
SELECT IdCity, CityName
FROM dbo.City
WHERE (CityName = N'Азов')
The problem is when creating the database from the beginning i had to choose the encoding language for the db, can't be changed after creating it though !

How to update specific value without updating a whole value in MySQL

I have a table like this
+-----+------------------+
| id | name |
+-----+------------------+
| 1 | John;Black;Mike |
+-----+------------------+
| 2 | White;Mike;John |
+-----+------------------+
| 3 | Jacob;Mike |
+-----+------------------+
| 4 | Will;Mason;Mike |
+-----+------------------+
as result of
SELECT * FROM people WHERE name LIKE '%Mike%';
Is there any query on how to update specific name Mike to Michael without updating a whole value. like John;Black;Mike to John,Black,Michael in all rows automatically.
You could use replace
update people
set name = replace( name, 'Mike', 'Michael')
where name LIKE '%Mike%';
anyway you should avoid storing comma separated value .. you should think to a proper normalized table for this data ..

Extracting data from column and put them in another column

I'm having a problem with building a query.
What I have is this in THE db
--------------------------------------
Id |name | profilenr | nr
--------------------------------------
1 | Harry| admin-124 | NULL
2 | Barry| admin-267 | NULL
6 | gerry| user-689 | NULL
9 | larry| user-435 | NULL
What I want to do is:
Getting only the numbers from the profilenr column and put them in the nr column of each profile that starts whit admin- .
In this example only for harry 124 in colum nr
And for Barry only 267 in colum nr.
I know this is possible but don't know how to build the query for this.
May be more like this:
update my_table update
set nr = substr(profilenr, locate('-', profilenr)+1, 3)
where profilenr like 'admin-%';
You can use a substr and locate
update my_table
set nr = substr(profilenr, locate('-',profilenr)+1, 3);