views:

46

answers:

2

Hi all, I'm using one of my MySQL database tables as an actual table, with times of the day as each column, and one column called day. You guessed it, in day it says the day of the week, and in the rest of the cells it says what is happening at that time.

What I want to do is only show the cells that have value in it. In my case, I'm always going to have all the rows and 2 columns full. The 2 columns are 'day' and '19:00', however in the future I might add values for '18:00' etc.

So, how can I only SELECT the columns and rows which have data in them? Some type of 'WHERE: there is data'?

Thanks!

EDIT: PictureDatabase

A: 

From what I gathere you are looking something along the lines of

WHERE col1 IS NOT NULL

But it would be helpful if you could elaborate more on your schema, especially if you could draw a sample table.

inflagranti
That sounds like what I want :). Do you want me to post a picture of what PHPmyAdmin is displaying?
Sam
Also, is there a way to say something like 'Where all columns except 'Day' IS NOT NULL'. I could just write it in, however it would be a very long query, where I believe a shorter one is possible?
Sam
Yeah, such picture may be helpful.
inflagranti
Added picture...
Sam
No, there is no way of making a condition for "all columns except day", you have to write it as `where not ([00:00] is null and [01:00] is null and [02:00] is null and ... and [21:00] is null and [22:00] is null and [23:00] is null)`.
Guffa
+2  A: 

Having time or day as columns means that you have data in your field names. Data belongs inside the table, so you should normalise the database:

table Calendar
--------------
Day
TimeOfDay
Appointment

This way you don't get a lot of empty fields in the table, and you don't have to change the database design to add another time of day.

Now you can easily fetch only the times that exist:

select Day, TimeOfDay, Appointment from Calendar
Guffa
It's not so much a calendar, as a 'schedule' for an online radio I am making (and all the different DJ's and when they are broadcasting). Also, I won't be needing to add new times or half an hour times etc.
Sam
Thanks for clarifying. I see, but it wouldn't be very friendly to view, and I want to create a table with the data.
Sam
@Sam: Creating a table from the data is something that you should do in the user interface. You shouldn't build the database based on how it's going to be displayed.
Guffa
I know it's unconventional but it is a good solution for what I'm trying to do.
Sam
@Sam It clearly isn't because from your linked question you are now stuck with writing a 24 column WHERE clause! At that point do you not consider there might be a better way? You can use a pivot query to bring it back in the format that is good for display but store it in a format that is easier to search.
Martin Smith