Solved

Develop a query exploding values in a column and comparing

Posted on 2013-02-03
5
212 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 109

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 109

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 109

Expert Comment

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

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

770 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