In my database table, Due to some mistake same row has been entered twice, But it is now having one column value with some appended numeric value in the duplicate entry.
For example,
If my table has a column named filename, Then in one row it has value 'some-random-name.pdf'.
And in the duplicate row, it has value 'some-random-name-1532.pdf'.
I need to identify all such records. Please note that there can be any or zero number of dash(-) in the filename. So Like query something like '%-____-%.pdf did not help me.
Assuming that complete filename contains only one dot between name and extension you may try this:
WHERE SUBSTRING_INDEX(t1.value, '.', -1) = SUBSTRING_INDEX(t2.value, '.', -1)
AND LOCATE(SUBSTRING_INDEX(t1.value, '.', 1), SUBSTRING_INDEX(t2.value, '.', 1)) = 1
Related
I have data stored in a column called supervisors_id in a table. The structure of data in this column is like this: 12, 44, 55, 32, 85, 75, 45. So I want to fetch the total number of ID's in that supervisors_id column in a row. My expected result, for instance in the above example should be 7. How can I do that with a MySQL query ?
You can count the commas by comparing the length of the string to the length of the string with commas replaced by the empty string.
Somewhat like:
SELECT length(supervisors_id) - length(replace(supervisors_id, ',', '')) + 1
FROM elbat;
If there are also empty strings in the column you might need to add some more logic, that checks for that and returns 0 instead of 1 in that case. For example by using a CASE:
SELECT CASE
WHEN supervisors_id <> '' THEN
length(supervisors_id) - length(replace(supervisors_id, ',', '')) + 1
ELSE
0
END
FROM elbat;
But your design is not the best. Instead of a comma delimited list, there should be a linking table.
My client had input both the city and state (ex: Atlanta, GA) in the city field. The city field is in the rmn_wpjb_job table in a column named job_city.
I want to be able to put the state portion of what the client had entered in the job_state column which has all empty rows right now. The end result would have just the city in the job_city column and the state in the job_state column. I would need to remove the comma in the job_city column.
My web hosting support gave me this query to start:
SELECT SUBSTRING_INDEX(job_city, ',', -1)
FROM `rmn_wpjb_job`
This works fine, but I want to take the results of this query and put it into the job_state column.
There are 500 rows that I need to do this for. Thanks in advance for the help.
You can perform an UPDATE operation
update rmn_wpjb_job
set job_state = SUBSTRING_INDEX(job_city, ',', -1)
where <some_condtion_if_needed>;
I would like you to run a script where the existing fields
d1_image_url
should be changed from URL to image name
For example
Following column contains
abc.com/r_doc/58/567889.jpeg
i want only 567889.jpeg will print.
Note- image name will be unique for 10 thousand records.
I am trying with following update query but its not giving me unique image name.
update cl_master set d1_image_url = substring_index('images/r_doc/58/567889.jpg','/',-1)
where the cl_master is table name and d1_image_url is column name.
can any one give me solution for this.
You should use your column name in substring function as parameter.
update cl_master set d1_image_url = substring_index(d1_image_url, '/', -1)
This should work:
update cl_master set d1_image_url = SUBSTRING_INDEX(d1_image_url, '\\', -1)
Good Evening Everyone;
I have a table in MySql that I would like to update as follows. Just as an FYI I have searched this topic and have yet to find a solution.
The table name is ALK_Results_NEW in a MySQl database, the data in the ICD9 column contains data seperated by commas.
I need to keep the first set in the ICD9 column so for example the first row has V57.9 , 246.9.
I need to keep the V57.9 in the ICD9 column and move 246.9 into the ICD9_SECONDARY column.
If a row has more than 2 then I need to move all other into the ICD9_OTHER column (This column can have mulitple ICD9 Codes separated by commas)
To summarize the first code needs to stay in the ICD9 column and the second set of codes needs to be moved into the ICD9_SECONDARY. After the data is moved the ICD9 Column should only have the forst set of codes.
Any help would be greatly appreciated.
Assuming the two columns already exist in the table, you can change the data using an update:
update alk_results_new
set icd9_secondary = substr(icd9, instr(icd9, ',') + 1),
icd9 = substring_index(icd9, ',', 1)
where icd9 like '%,%';
EDIT:
Oops, I didn't realize there were three columns. The approach is similar, but a little more complicated because you need to take into account the length of the strings. I think the following should do what you want:
update alk_results_new
set icd9_other = (case when icd9 like '%,%,%'
then substr(icd9, length(substring_index(icd9, ',', 2)) + 2)
end),
icd9_secondary = (case when icd9 like '%,%'
then substring_index(substring_index(icd9, ',', 2), ',', -1)
end),
icd9 = substring_index(icd9, ',', 1);
Note: test the logic out on a select before running the update.
I have a table with a column that has CSV.
TableA:
field_id | matches
---------------------
1 1,2,4,6,8,11,14,56
Now I need to get the field_id that matches a user given csv. So for instance, user string is 1,4,11, then it should return some value may be just true.
1.) Find_in_set does not work. Because it takes only one element and searches that in a SET/CSV column.
2.) Cannot use like concat('%,', user_input , ',%'). Because user input may not be in order.
Any other ideas? I guess this is a very common scenario.
Note: I dont need to search all records. I need to search a specific record. So in the above table, I just need to search one record that has field_id = 1. i.e. (where field_id = 1). (May not matter, but just an info)
Well, this is a good argument for having data in a proper relational form. But, you can try:
select t.*
from t
where (find_in_set($user_input, 1) = 0 or
find_in_set(substring_index(substring_index($user_input, ',', find_in_set($user_input, 1)), ',', -1), matches) > 0) and
(find_in_set($user_input, 2) = 0 or
find_in_set(substring_index(substring_index($user_input, ',', find_in_set($user_input, 2)), ',', -1), matches) > 0) and
. . .
Do this for however many values you might have in the userinput set.
I presume there is no straight solution with MySQL query like Find_In_Set. So I guess I will have to handle this with multiple queries or with Looping.