How can I get some random numbers from the database and add more than or equal to 100?

Source: Internet
Author: User
How do I randomly remove some numeric data from the database and add more than or equal to 100?


Reply to discussion (solution)

Find the problem seems a bit contradictory, change the method of asking

To take some numbers out of the database, the numbers add up to or equal to 100, without limiting the number taken out,
Example: can take out 50,50 these two numbers, add equals 100;
can also be removed 10, 20, 20, 50 These four numbers, add also equals 100;
can also be removed 10.5, 20.5, 30, 20, 26, these numbers, add more than 100;
Or take out other numbers that can be equal to or greater than 100 when added.

The number taken is random, no matter how many digits, as long as it can be equal to or greater than 100

However, once it is equal to or greater than 100, it is stopped out and the number already taken out is output to the page.

I'll give you a clue.
For the first time, remove the 100 strips directly.

Then one is judged whether the addition is greater than 100 if it is larger than the direct jump.


This kind of, only accesses once the database, saves the time.

Save time by accessing the database only once.

I don't know which brother has a better idea.

SELECT * from Tbl_name a WHERE (select sum (field) from Tbl_name where id>=a.id) > 100

Requires an ascending ID field to be auxiliary

I don't know what that means.

SELECT * from Tbl_name a WHERE (select sum (field) from Tbl_name where id>=a.id) > 100

Requires an ascending ID field to be auxiliary

I don't know what that means.


Too many products, there is no way to manually calculate the various combinations. In order to the premise of a variety of random combinations, to get the cost, ask price, profit, so as to be aware of, good promotion activities, but unexpectedly simple and effective method, I think this may take up a lot of server resources, so to ask there is nothing to improve the efficiency of a good way, but also please your senior colleagues.

You have to put your so-called inefficient method to good!

You have to put your so-called inefficient method to good!


Oh, I mean, I think the solution might take up a lot of server resources, not clearly expressed, offended.

Existing so-called inefficient code, but can solve the problem
Then it's possible to optimize the code to be efficient

You can't say exactly what you're going to do, how you're going to do it, how you might have an efficient code.

To take some numbers out of the database, the numbers add up to or equal to 100, without limiting the number taken out,
Example: can take out 50,50 these two numbers, add equals 100;
can also be removed 10, 20, 20, 50 These four numbers, add also equals 100;
can also be removed 10.5, 20.5, 30, 20, 26, these numbers, add more than 100;
Or take out other numbers that can be equal to or greater than 100 when added.
The number taken is random, no matter how many numbers, as long as it can be equal to or greater than 100, but, once equal to or greater than 100, stop the extraction, and the data has been removed to the page. The simpler the better.

That's what I want to ask.

It's not something that can be done by the database itself.
You need to combine the program with a read-only line.

A query command
A while loop with conditional judgment inside

I don't know how easy it is for you.

SELECT * from Tbl_name a WHERE (select sum (field) from Tbl_name where id>=a.id) > 100

Requires an ascending ID field to be auxiliary



How do I export this?

It's not something that can be done by the database itself.
You need to combine the program with a read-only line.

A query command
A while loop with conditional judgment inside

I don't know how easy it is for you.



I took care of, although the owner of the code is not directly used (no use of this writing, do not understand how to output,), but from the thought of a more simple than I wrote earlier, the original I used to write too around. , the following is the code
$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, also ask the owner to teach this how to output, thank you.

You're not what I'm talking about.
A query command
A while loop with conditional judgment inside
It?

As for that instruction, you'll know what's going on when you execute it in the manager.

  • 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.