Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL isset and not showing blank 'cells'

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

like image 213
Sam Avatar asked Jan 24 '26 03:01

Sam


2 Answers

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
like image 82
Guffa Avatar answered Jan 25 '26 18:01

Guffa


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.

like image 41
Janick Bernet Avatar answered Jan 25 '26 18:01

Janick Bernet