简体   繁体   中英

Creating mysql table of consecutive numbers

Hi i have created a table called allposs with all the possible lotto results from 6/49 lotto which seemed easy enough.

create table allposs (
`id` mediumint (8) unsigned NOT NULL,
`n1` tinyint (3) unsigned NOT NULL,
`n2` tinyint (3) unsigned NOT NULL,
`n3` tinyint (3) unsigned NOT NULL,
`n4` tinyint (3) unsigned NOT NULL,
`n5` tinyint (3) unsigned NOT NULL,
`n6` tinyint (3) unsigned NOT NULL,
key `id` (`id`)
) engine=myisam;

Created a procedure to generate the results and put them in the table which has worked fine.

Now i need to make other tables using the results from this table eg.

Create a table "filter1" that holds all results that are consecutive 1,2,3,4,5,6/44,45,46,47,48,49
and another "filter2" that has 5 consecutive 1,2,3,4,5,25,/ 1,10,11,12,13,14 then 4consec 1,2,3,4,13,23,/ 1,16,17,18,19.49 and so on.

Once I have created all these I need view/select "allposs" table -minus "filter1", "filter2" ect.

I am very new to mysql php and dont have money to pay a professional so I am trying to do it myself but I think its going to take forever as I have serched for past 2 weeks and cannot find the solution. I have very basic programming skills but are determined to learn. Please any help would be much apreciated. I am using mysql 5.1.54 on ubuntu 11.4

These will be rather slow queries because you are searching in a quite big table.

Find rows where all 6 numbers are consecutive.

Slow:

SELECT *
FROM allposs
WHERE n2 = n1+1
  AND n3 = n2+1
  AND n4 = n3+1
  AND n5 = n4+1
  AND n6 = n5+1

Faster:

Add a compound index (n1,n2,n3,n4,n5,n6) on your table.
Also add a (n2,n3,n4,n5,n6) index, a (n3,n4,n5,n6) one, etc..., up to a (n6) index.

Then, create a single table with just one column and 49 rows:

CREATE TABLE num
  ( i TINYINT
  , PRIMARY KEY (i)
  ) ;

Then use this:

SELECT 
      ap.*
FROM 
      allposs AS ap
  JOIN
      num
    ON (n1,n2,n3,n4,n5,n6) = (i,i+1,i+2,i+3,i+4,i+5)

Find rows where 5 numbers are consecutive.

Slow:

    SELECT *
    FROM allposs
    WHERE n2 = n1+1
      AND n3 = n2+1
      AND n4 = n3+1
      AND n5 = n4+1
      AND n6 > n5+1
  UNION
    SELECT *
    FROM allposs
    WHERE n2 > n1+1
      AND n3 = n2+1
      AND n4 = n3+1
      AND n5 = n4+1
      AND n6 = n5+1

Faster:

SELECT 
      ap.*
FROM 
      allposs AS ap
  JOIN
      num
    ON ( (n1,n2,n3,n4,n5) = (i,i+1,i+2,i+3,i+4) AND n6 > i+5 )
    OR ( n1 < i-1 AND (n2,n3,n4,n5,n6) = (i,i+1,i+2,i+3,i+4) )

You can use these queries with CREATE TABLE anothertable SELECT ... or with INSERT INTO anothertable SELECT ... variations to populate more tables if you want to have access to these rows without having to run the queries every time.

Because the numbers in the columns are in order - that is n1 < n2 etc - you can get the lot in one query:

select *
from (select
  id,n1,n2,n3,n4,n5,n6,
  (n1=n2-1) + (n2=n3-1)+ (n3=n4-1)+ (n4=n5-1)+ (n5=n6-1) as consecutive_count
  from allposs) x
) where consecutive_count > 0
order by consecutive_count desc, n1,n2,n3,n4,n5,n6;

This query will return all combinations with 6 consecutive numbers first, then all combinations with 5 consecutive numbers, etc and won't return any combinations that have no consecutive numbers.

Having got that you should be able to write your php to process the rows.

If you wanted to use separate queries, change the condition to where consecutive_count = 3 (for example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM