Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:41:34+00:00 2026-06-06T03:41:34+00:00

I need to store lotto numbers in a SQL database. There are 8 separate

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:41:37+00:00Added an answer on June 6, 2026 at 3:41 am

    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_tickets
      ----------------------
      id [PK]
      first_name
      last_name
      date_given
      ...
      
    • lottery_ticket_numbers: To store that corresponding lottery ticket’s numbers:

      lottery_ticket_numbers
      ----------------------
      id        [PK](FK)
      number    [PK]
      position  
      

    Connect the two on the id field via a 1:N identifying relationship, and have each lottery ticket have 8 corresponding rows in the lottery_ticket_numbers table. The position field 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:

    SELECT
        a.*,
        GROUP_CONCAT(b.number ORDER BY position SEPARATOR '-') AS lottery_number
    FROM
        lottery_tickets a
    INNER JOIN
        lottery_ticket_numbers b ON a.id = b.id
    WHERE
        b.number IN (07,12,34,40,59,80,88,89)
    GROUP BY 
        a.id
    HAVING 
        COUNT(b.number) = 8
    

    The result set will look something like this:

    id   |   first_name   |   last_name   |   date_given   |   lottery_number
    --------------------------------------------------------------------------------
    823  |   Zane         |   Bien        |   2012-01-01   |   34-80-07-89-12-40-59-88
    2321 |   Jeff         |   Clark       |   2012-01-14   |   59-07-88-40-12-34-80-89
    

    Note:

    • If you are using MySQL, the 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
      ----------------------
      id [PK]
      date_pulled
      
    • winning_numbers_numbers:

      winning_numbers_numbers
      ----------------------
      id     [PK](FK)
      number [PK]
      

    To query for the winning tickets:

    SELECT
        a.*,
        GROUP_CONCAT(b.number ORDER BY position SEPARATOR '-') AS lottery_number
    FROM
        lottery_tickets a
    INNER JOIN
        lottery_ticket_numbers b ON a.id = b.id
    WHERE
        b.number IN
        (
            SELECT number
            FROM winning_numbers_numbers
            WHERE id = <id of a particular lottery number>
        )
    GROUP BY 
        a.id
    HAVING 
        COUNT(b.number) = 8
    

    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:

    SELECT
        GROUP_CONCAT(b.number ORDER BY b.number SEPARATOR '-') AS winning_number,
        a.date_pulled
    FROM
        winning_numbers a
    INNER JOIN
        winning_numbers_numbers b ON a.id = b.id
    GROUP BY 
        a.id
    ORDER BY
        a.date_pulled DESC
    

    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 BY in the GROUP_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:

    SELECT
        a.*
    FROM 
        ticket_numbers a
    INNER JOIN
        winning_numbers b ON
            a.n1 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n2 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n3 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n4 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n5 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n6 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n7 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            a.n8 IN (b.n1, b.n2, b.n3, b.n4, b.n5, b.n6, b.n7, b.n8) AND
            b.id = <winning number id>
    

    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_CONCAT function to display the ticket numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store currency exchange rate data in the SQL 2008 database. I
I need to store objects that have a number (>2) of integer member variables
I need to store data in a grid like this: {0,1,2,3} {4,5,6,7} {8,9,10,11} {12,13,14,15}
I need to store a tree data structure in my database, for which I
I need to store large amounts of metering data in a database. A record
I have id values for products that I need store. Right now they are
I need to store a series of configuration values in a database. A couple
I need to store a lot of data coming in from a server into
I need to store a file, such that my ASP.Net MVC app can access
I need store multiple currencies on my database... Here's the problem: Example Tables: [

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.