Solved

Develop a query exploding values in a column and comparing

Posted on 2013-02-03
5
209 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
ID: 38849630
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
ID: 38854192
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
ID: 38854519
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
ID: 38854668
very good advice.
0
 
LVL 108

Expert Comment

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

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

912 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

16 Experts available now in Live!

Get 1:1 Help Now