I'm trying to figure out a safe way (if possible) to take the "url" column and copy it into the "company" column ... but I don't just want to copy the whole thing - let me try to explain.
I want to copy the company name like for example: apple, mircosoft..etc (from the path) and place them into their company column (to the left). I have about 5000+ results that need to be done and done safety. They all have the same file path structure with "../../images...."
Could I use something like UPDATE with SET?
UPDATE table SET company = url
Thanks for an feedback! I really appreciate it!
You can pick out the N left-most "fields" in a string separated by a character of your choice.
SELECT SUBSTRING_INDEX(url, '/', 4)
FROM mytable
LIMIT 10;
Returns:
../../images/apple
etc.
Then use a -1 to get the right-most field of that result to get the last one.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(url, '/', 4), '/', -1)
FROM mytable
LIMIT 10;
Returns:
apple
Once you are happy with the expression, use it in an UPDATE:
UPDATE mytable SET company =
SUBSTRING_INDEX(SUBSTRING_INDEX(url, '/', 4), '/', -1);
See https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index
In mysql 8 you can use the regexp_substr function to use a regular expression to find a substring. Something like:
SELECT REGEXP_SUBSTR(url, '[^/]+', 14) FROM table;
Which would find any character that is not a / but starting at character 14 (part right after your leading ../../...etc string.
Related
I have a table containing special data and I need to hide those data after I backup them
for example I have a filed containing phone number like this
0020158578939
I need to make it like this
002015*******
whats is the sql command I must use to do this.
I searched here but didn't find a suitable answer
thank you in advance
Assuming the column name is phone, this SQL statement will do:
SELECT CONCAT(SUBSTR(phone, 1, 6), REPEAT('*', CHAR_LENGTH(phone) - 6)) AS masked_phone
FROM `yourTable`
This will only show the first 6 characters and will mask the rest with *.
Update (as per your comment):
Upon backup, you could generate the following UPDATE statement to mask the phone number:
UPDATE `yourTable`
SET phone = CONCAT(SUBSTR(phone, 1, 6), REPEAT('*', CHAR_LENGTH(phone) - 6))
If you need this phone number again then use :
base64_encode('0020158578939');
and when you need it then
base64_decode('your mysql field value of phone');
Otherwise use :
md5('0020158578939'); sha1('0020158578939');
You can use this to generate the obscured string if the field is a character string:
SELECT RPAD(SUBSTR(PhoneNumber, 1, 6), Length(PhoneNumber), '*')
See this link how the RPAD function works specificly: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_rpad
I'm working on a project using the pages in php / mysql and html; I have a table that contains the data for calls made from a PBX and save the number called, the source, date, time, etc ... what I want to do is to search within this table all the phone numbers that have the first 4 digits equal to those that pass through the query, only that i have no idea how to pull off only the 4-digit or at least how to make a control character by character of the value contained in the field. I tell you now that the field is a varchar. Thank you in advance :)
To do that in MySQL query, either
SELECT *
FROM <tablename>
WHERE LEFT(<column>, 4) = "<4 digits>"
or
SELECT *
FROM <tablename>
WHERE <column> LIKE "<4 digits>%"
or in the PHP side :
if (strpos($column,'<4 digit>') !== false) {
echo 'true';
}
Use this, to get substring
SELECT aut_name,
RIGHT(aut_name,7)
FROM author
WHERE country='UK';
See more at: http://www.w3resource.com/mysql/string-functions/mysql-right-function.php#sthash.xKNwZeki.dpuf
I suggest this solution:
$variableWhereYoustoreTheFourDigits="1234"; //Use whatever you have in your code to set the value.
$result =$mysqli->query("SELECT number FROM yourtable where number LIKE \"$variableWhereYoustoreTheFourDigits%\");
I have a ref field in my mysql table that holds values that look like '0-0-at-3267-201411041356'. The first part (0-0-at-3267-) varies in length and values and the second part (201411041356) is a date/time that references a creation date/time. Everything that this code is used for is checked to see if it falls within a certain date/time period, such as between 201409010000 and 201508312359.
Normally I can simply explode the data and then measure but for this instance it would make it too clunky. So what I want to do is use the LIKE function in my query like so LIKE '%201411041356' but I want to use it with the > and < symbols, so the full query looking something like SELECT * FROM my_table WHERE ref LIKE > '%201409010000' AND ref LIKE < '%201508312359'
Any ideas would be most welcome! BTW, this has always been the way this data has been stored and there's lots of it so changing it is not an option.
Could you use between on a substring, so something like:
SELECT * FROM my_table WHERE SUBSTRING(ref,-12) BETWEEN '201409010000' AND '201508312359'
SELECT *
FROM my_table
WHERE SUBSTRING(ref,-12) BETWEEN '201409010000' AND '201508312359'`
would work better for substracting last 12 characters.
Because You wrote that first part may have different lenght so You have strip substring starting from the end.
So Your quwery may looks like:
SELECT * FROM table
WHERE
SUBSTRING('ref', -12) > $from
AND
SUBSTRING('ref', -12) < $to
If you just want to measure by date use:
left(right(ref, 12), 8)
I have a CMS with a bunch of different tags and categories. I obviously use each category as a unique ID and save them in the database like so:
cats -> 2,15,115
tags -> 13,33,113
That is a simple example.
I am having problems when I want to show related content by a GET variable $cat to use in my query:
WHERE cats LIKE '%$cat%'
Here is the challenge, if the $cat = '5' , then it returns, 5, 15, 55, 115... and so on. I just need it to match just '5' and nothing else!
I'm sure I am missing something really, really simple.
EDIT:
The find_in_set works really well. However, my other challenge is my $GET variable is sometimes like this: $cat = 150,181
So how can I use the variable $cat to see if there is just one of those matches ? I want to match either 150 OR 181 against the query ?
you can use FIND_IN_SET() since column cats has these values 2,15,115
SELECT * FROM tableName WHERE FIND_IN_SET('5', cats) > 0
MySQL FIND_IN_SET
If you use LIKE with wildcard character '%' you extend your search all values containing '5' in base of you put your %.
%value% (cointaining)
%value (end with value)
value% (start with value)
You can use like as equal, simply not used %, but I advice you to use equal operator (=)
So I have a 625 digit long number to store in MySQL containing only 0 1 and 2 digits. The best way I've found to store it so far is as VARCHAR(625). I suspect that there are better ways to do it, I'm just not sure how.
I want to run a query such as "change the 128th character of the text to 1" without having to query the text into PHP, change it from there, and write the new text into the DB.
So the question is: what's the simplest method of doing this, and what field type is optimal for this kind of stuff?
Thanks in advance.
If you're going to be doing things like "change the 128th character of the text to 1" then I'd recommend keeping the data type as VARCHAR(625). If the length will always be 625 then you could use CHAR.
This will change the 128th character to 1 in MySQL:
UPDATE your_table set your_column =
CONCAT(SUBSTRING(your_column, 1, 127), '1', SUBSTRING(your_column, 129))
You can user INSERT() String function
give it a try
UPDATE tbl set field = INSERT(field,128,1,'1') where id = primary key
If you're only storing numbers, why not use int(625) instead of varchar?
You can use a replace command, but you'll have to know what to replace with
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
If you don't know what exactly you're replace with, you can replace the "replace found string with this string" with a sub query. Using length and substr to split, replace what you want, and concat back together.
This example should make what I'm saying make more sense
http://sqlfiddle.com/#!2/dbc21/2/0quey
A complex alternative:
Replacing 5th character to 'r' where primary key is 30:
update `myTable` set `myField`= regexp_replace(`myField`, '.', 'r', 5, 1) where `id`=30 limit 1