Solved

Develop a query exploding values in a column and comparing

Posted on 2013-02-03
5
207 Views
Last Modified: 2013-02-05
I have a table contest_setup and it contains (simplified)

id
account_id
ContestName
ContestStartDate

the ContestStartDate column contains dates that a particular contest will run ie:
2013-02-18 00:00:00,2013-02-19 04:00:00,2013-02-20 16:00:00
(this contest would run on 3 different dates for example)

Some rows may contain just one date, others a few like above.

My question is: how can I construct a query to check for all the Contests that may be running on 2013-02-19 (from 00:00:00 to 23:59:59) and arrange them by date/time soonest to latest?

I'm guessing I need to go through the rows that belong to a certain account_id that would be passed to it (for example '17'),

I need to explode the ContestStartDate on the comma and check the values for the date I'm interested in and somehow 'magically' display them earliest to latest.

Can anyone throw me a bone to get going in the right direction? Its beyond my ability currently.
0
Comment
Question by:tjyoung
  • 3
  • 2
5 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Make a Google search for "Should I normalize my database" and read the interesting opinions on either side of the idea.  In the instant case, the answer for you is "Yes, I must normalize."  Any column that contains more than one atomic data element is a recipe for catastrophe.  So create a "junction table" to connect your contest_setup table to your contest_dates table.  Put all the dates in a row of the contest_dates table, using the column data type of DATETIME.
0
 
LVL 1

Author Comment

by:tjyoung
Comment Utility
Hi,
changed my approach but not sure about the junction table or why to use one?
Here is where I'm at now:

2 tables
contest_setup - id (auto), contestname, account_id
contest_dates - contestsetup_id, contestdate(datetime)

So I'm thinking I would be able to query contests slated for today for example by joining the tables and matching up the id and the contestsetup_id where its todays date and the account_id is 'x'

That right? I wasn't sure how or why you would introduce a junction table.

I haven't got to querying the tables etc. yet so if there is a fatal flaw, I may have just not come across it at this point.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I don't see anything wrong with that design.  You may find that you do not need a junction table and you can just use the JOIN clause.  Be sure to index any columns used in JOIN, just as you would index any columns used in WHERE, and add LIMIT statements if possible.

Best regards, ~Ray
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
very good advice.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points, it's a great question, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now