I have the following mysql table
+----------------+--------------------+
| ID | picture |
+----------------+--------------------+
| 10954 | picture/mypics.jpg |
+----------------+--------------------+
| 10759 | picture/mypics2.jpg|
+----------------+--------------------+
I need the existing filenames to be renamed to the ID for instance mypics.jpg renamed to 10954
How can i achieve this?
Thanks
UPDATE `following mysql table`
SET picture = CONCAT (
SUBSTR (picture, 0, CHAR_LENGTH(picture) - LOCATE('/', REVERSE(picture)),
'/',
ID
)
This replaces the part after the last slash with the value from the ID column. To my knowledge, MySQL has no function to find the last occurrence of a substring within a string, hence the funny construction with CHAR_LENGTH, LOCATE and REVERSE.
See MySQL String Functions for details.
Related
My team has stored array data as a string in MySQL like below
["1","2","22","11"]
How can we select data from the table where the column contains a certain branch number.
Example of table
sno | Name | Branch
1. | Tom. | ["1","2","22"]
2. | Tim. | ["1","2"]
Can you suggest a query to select all rows containing branch 2?
We tried using FIND_IN_SET() but that is not working as the double quotes and square brackets are also a part of string.
Use like:
select *
from mytable
where Branch like '%"2"%'
I would like to create a new column in a MYSQL table based on the string values in an existing column.
My strategy is to first create an empty column and then update the values in the new column based on values in the existing column. However, I am stumbling on how to parse the string in order to extract the correct values.
The string is of the form 1.1.25. I want to extract the value before the first period and the value between the two periods and put these in new columns.
mytable
id|actsceneline|text
1 |1.1.1 |How are you.
1 |1.1.2 |Not bad. You?
To create the new empty column
ALTER TABLE mytable
ADD COLUMN act VARCHAR(6) NOT NULL,
ADD COLUMN scene VARCHAR(6) NOT NULL
To change the values in the new columns, I imagine I would do something like:
UPDATE mytable SET act = '1',scene = 1
And then use MYSQL string functions such as instr or substr or regex to extract the values and update the new columns as in.
UPDATE mytable SET act =
SELECT SUBSTR(actsceneline, 1, LOCATE('.', text)) FROM mytable
However, I'm struggling with how to extract the values from the string.
Thanks for any suggestions.
Try using SUBSTRING_INDEX():
UPDATE mytable
SET act = SUBSTRING_INDEX(actsceneline, '.', 1),
scene = SUBSTRING_INDEX(SUBSTRING_INDEX(actsceneline, '.', 2), '.', -1);
Result given your data:
mysql> select * from mytable;
+----+--------------+---------------+-----+-------+
| id | actsceneline | text | act | scene |
+----+--------------+---------------+-----+-------+
| 1 | 1.1.1 | How are you. | 1 | 1 |
| 2 | 1.1.2 | Not bad. You? | 1 | 1 |
+----+--------------+---------------+-----+-------+
Best way to create a select and what you want to update.
create a new table from your existing table.
"create table destinationtablename
select * from sourcetable;"
then work on your destinationtablename.
All work finished then check twice before update to original table or you can also take backup of your data by creating new table.
We currently have a database that has stored over 100,000 records of data over the years however in a structure that does not work anymore.
There is a field in the table called youtube_video
It has been storing all of the embed YouTube videos like this:
http://www.youtube.com/embed/3mHuu5NklOs?rel=0
http://www.youtube.com/embed/3mHuu5NklOs
We need to change it to:
https://www.youtube.com/watch?v=3mHuu5NklOs
Is there a way to write a query that makes this change with a single query?
You can use REGEXP_REPLACE :
SELECT REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
) FROM mytable
Regex breakdown :
^ : start of string
http://www.youtube.com/embed/ : constant string part
([^?]+) : as many consecutive characters as possible others than a question mark ; the surrounding parentheses capture that part of the string, and make it available as \1 in the second argument to REGEXP_REPLACE()
.* : anything (until end of string)
This demo on DB Fiddle returns :
| youtube_video | new_youtube_video |
| ---------------------------------------------- | ------------------------------------------- |
| http://www.youtube.com/embed/3mHuu5NklOs?rel=0 | https://www.youtube.com/watch?v=3mHuu5NklOs |
| http://www.youtube.com/embed/3mHuu5NklOs | https://www.youtube.com/watch?v=3mHuu5NklOs |
If needed, you can easily turn this into an UPDATE :
UPDATE mytable
SET youtube_video = REGEXP_REPLACE(
youtube_video,
'^http://www.youtube.com/embed/([^?]+).*',
'https://www.youtube.com/watch?v=\1'
);
I'm storing permissions into DB with Array JSON String, and i want select them by permission specific permission. at this time I'm selecting them like this:
1 | Dog | [3,4]
2 | Cat | [33,4]
3 | Tiger | [5,33,4]
4 | wolf | [3,5]
SELECT * FROM `pages` WHERE access REGEXP '([^"])3([^"])'
it works but not as it should work. This query gives me all records which contains 3 but also it gives which contains 33. my question is how i must format my regexp to get row by specific value into json string.
p.s i have mysql 5.5 so as i know on this version json functions is not supported
If you only have numbers in the fields, you can alter your regexp to only take values where the string you are looking for (here the '3') does not have another number immediately close to it :
SELECT * FROM `pages` WHERE access REGEXP '([^"0-9])3([^"0-9])'
REGEXP '[[:<:]]3[[:>:]]'
That is, use the "word boundary" thingies.
I have a Mysql table that looks like this:
+-------+
| NAME |
+-------+
| James |
| Alex |
| Jones |
| ... |
+-------+
Each name is unique.
And I have a txt file that is a list of names that needs to be imported into this table.
The list needs to be imported keeping the order of the names, but when I use Phpmyadmin to import it, the list seems to get sorted by name prior to being imported.
How can I prevent this behavior? I just need it to be imported as is, without any change. And when I query it should return the results at the same order I inserted them.
by default the data being returned from mysql are ordered prior to an indexed column (phpmyadmin has nothing to do with the ordering) if you define an index on the table using the name column the table results will be sorted according to the index of this column but if you want to order them prior to the last insert you have, order them by ID only if you set the ID as an auto_increment column in the design cuase it will automatically increment the values by 1 for the newly inserted rows. :)
Give each record an ID before importing, then you can sort by ID to get original layout