How can I randomly retrieve some numbers from the database and add them to a value greater than or equal to 100?

Source: Internet
Author: User
How can I randomly retrieve some numbers from the database and add them to a value greater than or equal to 100? How can I randomly retrieve some numerical data from the database and add the data to a value greater than or equal to 100?


Reply to discussion (solution)

It seems that the problem is a bit contradictory. ask another question.

To extract some numbers from the database, the result of adding these numbers must be greater than or equal to 100, without limiting the number of retrieved numbers,
For example, 50 and 50 digits can be taken out, and the sum is equal to 100;
You can also extract the four numbers 10, 20, 20, and 50, and the sum is equal to 100;
You can also extract the numbers 10.5, 20.5, 30, 20, and 26. The sum is greater than 100;
You can also retrieve other numbers that can be equal to or greater than 100 after addition.

The number is random. no matter how many digits are there, it can be equal to or greater than 100.

However, once the value is equal to or greater than 100, the extracted number is stopped and output to the page.

Give you a thought.
For the first time, retrieve 100 items.

Then, judge whether the sum is greater than 100. if the sum is greater than, jump out directly.


This method only accesses the database once, saving time.

Access the database only once, saving time.

I don't know which senior engineer has a better solution.

Select * from tbl_name a where (select sum (field) from tbl_name where id> = a. id)> 100

The id field must be in ascending order.

I don't know what it means to do this.

Select * from tbl_name a where (select sum (field) from tbl_name where id> = a. id)> 100

The id field must be in ascending order.

I don't know what it means to do this.


There are too many products and there is no way to manually perform a variety of combined computing. In order to get the cost, price, and profit under the premise of a variety of random combinations, so that you can be aware of it, do you have a good promotion, but you can't think of a simple and effective method, I think this may temporarily use a lot of server resources, so I would like to ask if there is any good way to improve efficiency. please give your own opinions.

You should do your so-called inefficient method first!

You should do your so-called inefficient method first!


Oh, I mean, the method I 've tried may occupy a lot of server resources. I'm not clear, so I'm offended.

Currently, the so-called inefficient code can solve the problem.
Then it can be optimized to efficient code.

You cannot tell exactly what you want to do, how to implement it, and how to have efficient code.

To extract some numbers from the database, the result of adding these numbers must be greater than or equal to 100, without limiting the number of retrieved numbers,
For example, 50 and 50 digits can be taken out, and the sum is equal to 100;
You can also extract the four numbers 10, 20, 20, and 50, and the sum is equal to 100;
You can also extract the numbers 10.5, 20.5, 30, 20, and 26. The sum is greater than 100;
You can also retrieve other numbers that can be equal to or greater than 100 after addition.
The retrieved number is random. no matter how many digits there are, it can be equal to or greater than 100. However, once it is equal to or greater than 100, it is stopped, and output the retrieved data to the page. The simpler the solution, the better.

This is what I want to ask.

This kind of thing cannot be done by the database itself.
You need to combine the program to read one by one

A query command
A while loop with internal conditional judgment

I don't know how easy you are.

Select * from tbl_name a where (select sum (field) from tbl_name where id> = a. id)> 100

The id field must be in ascending order.



How can I output this?

This kind of thing cannot be done by the database itself.
You need to combine the program to read one by one

A query command
A while loop with internal conditional judgment

I don't know how easy you are.



I got it done. Although the code of bamboo didn't be used directly (I didn't use this method and couldn't understand how to output it), I thought of a simpler one than I did before, it turns out that my previous writing method is too messy ., The following code is used:
$rm=mysql_query("SELECT * FROM `pql_spices` ORDER by rand()");while ($rmx=mysql_fetch_array($rm)) {  $Total+=$rmx['Price'];  if ($Total>=100)break;  echo $rmx['id']."、".$rmx['title'];  echo "
"; }echo $Total;


Select * from tbl_name a where (select sum (field) from tbl_name where id> = a. id)> 100
If you can, please teach me how to output this. thank you.

This is not what I said.
A query command
A while loop with internal conditional judgment
?

As for the command, you can execute it in the manager to know what is going on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.