Question

I have a table which contains 13 columns (ID, name, image, day1-10). The problem is: Most of the times I will have null columns. I was wondering how I could make so that my mysql query would get only the non-null columns, if that is even possible.

What I mean is, there won't always be day2-10 sometimes only day1 will be set and I would only want this value from this row. Is it possible? I tried googling it but didn't find anything that helped, I could only find ways to skip the entire row altogether

I would have to use PHP to fetch the results from this query.

I tried different queries but none of them worked

No correct solution

OTHER TIPS

Instead of

Table Main

ID | name | image | day1 | day2 | day3 ...
-------------------------------------------
1  | Foo  | img1  | 7    | NULL | NULL
2  | Bar  | img2  | 9    | 13   | 14

You should normalize your data into two tables like this:

Table Main

ID | name | image 
------------------
1  | Foo  | img1
2  | Bar  | img2


Table Days

ID  | MainID | Day
------------------
100 | 1      | 7
101 | 2      | 9
102 | 2      | 13
103 | 2      | 14

Then, you will use a JOIN to select all of the days that apply to whichever element from the Main table you're interested in.

An additional benefit here is that you're no longer constrained to just 10 days.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top