I need to store lotto numbers in a SQL database. There are 8 separate numbers that make up the lotto number.
EX: 07-12-34-40-59-80-88-89
I first thought to store this data in a single row with columns named lotto1 – lotto8. This makes sense because lotto numbers are always displayed low to high, and users will need to see the stored lotto numbers. Writing a select statement would be trivial.
I also need to see how many of the lotto numbers are winners. To do this I need to search for each winning number in every single column.
EX: If the winning numbers were 01-02-03-04-05-06-07-08, I would need to check every column to see “07” is in my lotto1 column.
Checking for winners would be easier if all lotto numbers were stored in a 1 column, and they had a LottoTicketID to associate the 8 rows of data with 1 ticket. When checking for winners, I don’t need to say which specific numbers/positions were matches, just how many matches there were.
SELECT LottoTicketID,Count(LottoTicketID) from LOTTOTABLE
WHERE LottoNumber in (01,02,03,04,05,06,07,08)
GROUP BY LottoTicketID
Storing this way would cause problems because I would need to write 7 joins just to display the numbers.
Can someone suggest a way to store this data so that I can:
1. Quickly displayed the lotto numbers/entires
2. Check for winners
The way you’ll probably want to tackle this is to have two tables to store information about lottery tickets:
lottery_tickets: To store information about each individual lottery ticket:
lottery_ticket_numbers: To store that corresponding lottery ticket’s numbers:
Connect the two on the
idfield via a 1:N identifying relationship, and have each lottery ticket have 8 corresponding rows in thelottery_ticket_numberstable. Thepositionfield will keep track of each numbers’ position (1-8) in the whole string of numbers.What this will allow you to do is easily figure out the winning lottery tickets by JOINing the tables together and narrowing it down to which lottery tickets have ALL the matching numbers:
The result set will look something like this:
Note:
GROUP_CONCAT()function consolidates the winning tickets’ numbers in the format XX-XX-XX-XX-XX-XX-XX-XX with numbers in their original order that the person got it. If you’re not using MySQL, I’m not sure if similar function exists in other DBMSs.Now you’ll probably want to store data about the actual winning lottery numbers as well. For this, you will also utilize two tables in much the same way: one for the lottery number and another for its corresponding numbers:
winning_numbers:
winning_numbers_numbers:
To query for the winning tickets:
Nearly the same as the previous query, except that now the number list is a result set from a subquery.
Displaying the winning lottery numbers is simple. Ex:
Retrieve all winning numbers and their pull date; displaying their number string as numbers in ascending order, and ordering by the most recent pulled number:
Note that when you insert winning numbers, you won’t have to worry at all about the order of the numbers which you insert. Since they are always displayed in ascending order, the
ORDER BYin theGROUP_CONCAT()takes care of that for you.Now let’s take a look at what you’ll have to do if you use the column-based approach (storing winning numbers and ticket numbers as single row but with eight columns housing each number):
Assuming the schema:
winning_numbers(id, date_pulled, n1, n2, n3, n4, n5, n6, n7, n8)ticket_numbers(id, fname, lname, date_given, n1, n2, n3, n4, n5, n6, n7, n8)Find all winning tickets for a given lottery number:
That’s one heck of a lot of
INs if you were to ask me…!!!The advantage of this approach is that you wouldn’t need the MySQL-specific
GROUP_CONCATfunction to display the ticket numbers.