I have a set of image paths stored in a table like this:
+---------------------------------------------------------+
|ID |pimg0 |pimg1 |pimg2 |
+-------------+-------------+--------------+--------------+
|1 |path/to/img0 |path/to/img1 | |
+---------------------------+--------------+--------------+
|2 |path/to/img0 |path/to/img1 | |
+---------------------------+--------------+--------------+
and I want to get the table name of the empty field so I can put a new entry into it.
I have tried nesting IFNULL() commands in the mysql query, which didn’t work:
IFNULL(pimg0, IFNULL(pimg1, IFNULL(pimg2, IFNULL(pimg3, IFNULL(pimg4, IFNULL(pimg5))))))
I have also tried some case blocks which didn’t work. Ideally I would like my query to return “pimg2” in the above scenario, but if it was to return “pimg1” I could easily increment it.
EDIT: Edited the table above, to clarify.
You’re experiencing one of the pain points of repeating groups across columns, which is a violation of First Normal Form.
The pain goes away if you normalize, by creating a child table with one column for image path, and adding multiple rows when you have multiple images.
Structuring a database in this way makes it easier to do many tasks:
owner_id.owner_idowns it.owner_id.