Random Number in MySQL column [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to add random number in one of my database column called likes which have about 5000 row in it. currently I have tried like below to get random number from 10 to 50.
$randomnumberlike = (rand(10,50));
but this is setting same number in all row...instead I want different number in row...anyone can please suggest how can I do it ?
Thanks

Try: UPDATE Table SET fieldName = ((rand() * 1000) % 40) + 10;
Because when you preset it in a variable, it'll be the same for all...

You're setting a variable:
$randomnumberlike = (rand(10,50));
If you're then using $randomnumberlike to add to the db then it will always be the same, its already be assigned (unless within a loop), you need to use (rand(10,50)) (or working similar) directly in the query like this:
UPDATE <table_name> SET <field_name> = ROUND((RAND() * (50-10))+10) WHERE ...;

Related

Is there a better way to write REGEXP in sql to select unique transaction number R-0001 and R-AD-0001 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Am having a lot of transaction number store in my db, example I-0001, IN-0001, N-0001, S-0001, SN-0001, R-0001 , R-AD-0001, and R-AA-00001.
Every time a new row insert, I will select the maximum number of transaction numbers and + 1 to keep track of every transaction number.
Here is my initial SQL SELECT MAX(`transaction_invoice`) FROM sw_invoice WHERE `transaction_invoice` LIKE '%N-%', but I found out using LIKE to select for example maximum number of N-, LIKE SQL will also select N-0001 and SN-0001 together
Example of LIKE SQL result
To solve this I found out REGEXP might solve my issue here is example SQL
SELECT * FROM `sw_invoice` WHERE `transaction_invoice` REGEXP '^N-', this SQL works well when select maximum number of N-, but when its come to select R-0001, it will also select R-AD-0001, and R-AA-00001 which start with R-
Example of REGEXP SQL result
Is there a way any SQL can only select the maximum transaction number I require? for example if I select transaction number start with R-, its only return the maximum number of R- not R-AD-
Just remove the leading wildcard from the like:
SELECT MAX(`transaction_invoice`)
FROM sw_invoice
WHERE `transaction_invoice` LIKE 'N-%';
You don't need regular expressions for this.
If you need to handle hyphens in the first part, then you need regular expressions:
where transaction_invoice regexp '^N-[0-9]+$'

how to remove spaces from a field in mySQL db? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a database where i store my photos title, tags, and their path on sever some of the records have names like:
photo 1234.png. I need to make them like photo1234.png
Why can't I use a query like
UPDATE tblPhoto a
set a.photoLink = replace(a.photoLink , ' ', '')
where a.photoLink like '% %';
And which is the best way to rename them in Linux Server, can I use php ?
You don't need where clause
UPDATE tblPhoto SET photoLink = REPLACE(photoLink , ' ', '');
For replacing the file name on your Linux Server you can try to look in this answer.
https://stackoverflow.com/a/2709619/7921383
Use php method for example:
$old_name="Hello World";
echo str_replace(" ","",$old_name);
//output Helloworld

Replace string in mysql- href [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Good day to all!
How can I change, in mysql db, something like
href="/ with href="http://something/. I have problems with ".
Thank you.
UPDATE table SET column=REPLACE(column,'oldstring','newstring')
If you are trying to deal with the double quotes I think you have 2 easy options:
1) Wrap them in single quotes like this:
UPDATE my_table
SET my_field = 'href="http://mysite...something/'
WHERE my_field = 'href="http:/';
2) Escape them with backslashes like this:
UPDATE my_table
SET my_field = "href=\"http://example.com/\""
WHERE my_field = "href=\"http:/";

Replace character between numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I converted phpbb2 forum to phpbb3.
But I have problem with phpbb2 posts links which remained in MySQL database.
phpbb2 posts links are eg.
/viewtopic.php?p=106352#106352
and phpbb3 are:
/viewtopic.php?p=106352#p106352
(there is letter p after #)
Current links from phpbb2 are not working, after convert,
so I need help replacing # between posts id (numbers) in MySQL DB.
I got a lot of links like:
/viewtopic.php?p=106352#106352
and I need to replace # with p ( add p at the end)
like:
/viewtopic.php?p=106352#p106352
I don't know much MySQL, so I stuck.
Please help
One possible approach:
UPDATE phpbb_posts
SET post_text = REPLACE(post_text, '#', '#p')
WHERE post_text REGEXP '^[^#]*p=[0-9]+#[0-9]+$'
WHERE clause is specified to prevent updating links that do not follow the format.
SQL Fiddle.
select replace('/viewtopic.php?p=106352#106352','#','#p')
update myTable
set myColumn = replace(myColumn, '#', '#p')
where someColumn = someThing

How to fetch mysql data having brackets? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to fetch mysql data having round brackets e.g. ABCD(XYZ). When I run query
"SELECT * FROM tablename WHERE Column = 'ABCD(XYZ)'"
it returns an empty result. Please suggest a way. Thanks in advance!
this should work:
INSERT INTO `tablename` (`Column`) VALUES ('ABCD(XYZ)');
SELECT * FROM `tablename` WHERE `Column` = 'ABCD(XYZ)'";
Maybe 'ABCD(XYZ)' is not exactly the value of your data (for example if you inserted some whitespaces before or after it.)
You can try it with a like to find that out:
SELECT * FROM `tablename` WHERE `Column` LIKE '%ABCD(XYZ)%'";
Another possibility is that your value has been converted with htmlentities and you saved something like this:
'ABCD&40;XYZ&41;'
&40; Left parenthesis
&41; Right parenthesis