If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Question about sums

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-03-06, 16:06
gorivo gorivo is offline
Registered User
 
Join Date: Apr 2006
Posts: 5
Exclamation Question about sums

Hello,

I have mysql table, named "countries":

country | number
-----------------
finland | 40
germany | 30
belarus | 15
france | 25
swiss | 21

I need to select 3 countires from this table randomly where the sum of numbers is less then 80.

For example

Good example:
finland(40) + belarus(15) + swiss(21) = 76
( 76 is less than 80 )

Bad example:
finland(40) + belarus(15) + germany(30) = 85
( 85 is greater than 80 )

so how to do it ?

I hope you understood my question.
Waiting for your answers!
Thank you in advance!
Reply With Quote
  #2 (permalink)  
Old 04-04-06, 12:20
gorivo gorivo is offline
Registered User
 
Join Date: Apr 2006
Posts: 5
anyone?
Reply With Quote
  #3 (permalink)  
Old 04-04-06, 13:35
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
have a look in the Mysql manual for random
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #4 (permalink)  
Old 04-04-06, 14:27
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Are you allowed to pick the same country more than once (in other words, is Belarus, Belarus, Belarus a valid combination for you)? Describe what you mean by random, since I don't think that even 1 percent of the folks that use that term in a problem description really mean random.

You'll probably get a lot better response if you describe your real problem, in real world terms. This description sounds like a "slipery slope" where every answer is wrong, but you can't find that out without trying at least a dozen answers first.

-PatP
Reply With Quote
  #5 (permalink)  
Old 04-04-06, 15:38
gorivo gorivo is offline
Registered User
 
Join Date: Apr 2006
Posts: 5
Hello,

first of all, thank you for your replies!
second of all, i am not allowed to pick the same country more than once (the column 'country' is unique).

To make you understand the problem, here is mysql query, which selects 3 countries by random without any sums:

Code:
SELECT * FROM `countries` ORDER BY rand() LIMIT 3
The valid result of this query may look like this:

country | number
=============
finland | 40
belarus | 15
germany | 30

As we can see, the sum of field, named "number", is 85 (40 + 15 + 30).

So, what i need is to add one more condition to query above: the sum of randomly selected numbers must be less than 80. In this case, finland(40) + belarus(15) + germany(30) = 85 is not valid result, because 85 is greater than 80.

In other words, mysql query should look something like this:

Code:
SELECT * FROM `countries` 
WHERE sum_of_all_three_numbers_selected_randomly < 80 
ORDER BY rand() LIMIT 3
I think subqueries must be used, but i'm not sure

I hope now you understood my problem
Waiting for your replies
Reply With Quote
  #6 (permalink)  
Old 04-04-06, 16:29
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
Can this be done without a stored procedure or some other sort of scripting?

I was thinking of having subqueries which successively selected "random" countries less than the remaining difference from 80 (that made sense...right? ) - but this could potentially leave you with one, or even, two nulls.
Reply With Quote
  #7 (permalink)  
Old 04-05-06, 13:40
gorivo gorivo is offline
Registered User
 
Join Date: Apr 2006
Posts: 5
any ideas how to do it without stored procedure or some other sort of scripting?
Reply With Quote
  #8 (permalink)  
Old 04-05-06, 14:04
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
I don't have a MySQL engine to test against here at work, but the following works fine in Microsoft SQL 2000. You should be able to adapt it fairly easily:
Code:
CREATE TABLE #dbforums (
   Country	VARCHAR(20)
,  dbforums	INT
   )

INSERT INTO #dbforums (country, dbforums)
   SELECT 'finland', 40 
   UNION SELECT 'germany', 30 
   UNION SELECT 'belarus', 15 
   UNION SELECT 'france', 25 
   UNION SELECT 'swiss', 21

SELECT *
   FROM #dbforums AS a
   JOIN #dbforums AS b
      ON (a.country < b.country)
   JOIN #dbforums AS c
      ON (b.country < c.country)
   WHERE a.dbforums + b.dbforums + c.dbforums < 80
It produces:
Code:
country              country              country              
-------------------- -------------------- -------------------- 
belarus              finland              swiss
belarus              france               germany
belarus              france               swiss
belarus              germany              swiss
france               germany              swiss
-PatP
Reply With Quote
  #9 (permalink)  
Old 04-05-06, 14:11
gorivo gorivo is offline
Registered User
 
Join Date: Apr 2006
Posts: 5
Thank you, Pat!
Reply With Quote
  #10 (permalink)  
Old 04-05-06, 15:00
jfulton jfulton is offline
Registered User
 
Join Date: Apr 2005
Location: Baltimore, MD
Posts: 297
Quote:
Can this be done without a stored procedure or some other sort of scripting?

I was thinking of having subqueries which successively selected "random" countries less than the remaining difference from 80 (that made sense...right? ) - but this could potentially leave you with one, or even, two nulls.
...wow, brain fart...

Populate and use a lookup table like Pat said...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On