I am sure there is a way to do this, but being that I am still new to MySQL and PHP, I can’t figure this out.
I have a table where the first column (base_folder) represents a folder, there are multiple images in that folder (sub_folder). The last field (layer) represents the image order. What I need to get is the max number from the layer column where the base_folder, the sub_folder, image_, image_type and view are identical.
Does anyone have any idea how to do to that?
Here is the table:
+-------------------------------------------------------------------------+ | BASE_IMAGE | +-----+-------------+------------+-------------+----------+-------+-------+ | id | base_folder | sub_folder | image |image_type| view | layer | +-----+-------------+------------+-------------+----------+-------+-------+ | 1 | CUP0001 | F | B_FF_0.png | B | FF | 0 | +-----+-------------+------------+-------------+----------+-------+-------+ | 2 | CUP0001 | F | B_FF_0.png | B | FF | 1 | +-----+-------------+------------+-------------+----------+-------+-------+ | 3 | CUP0001 | F | B_FF_0.png | B | FF | 2 | +-----+-------------+------------+-------------+----------+-------+-------+ | 4 | CUP0001 | F | B_FF_0.png | M | FF | 0 | +-----+-------------+------------+-------------+----------+-------+-------+
What I would like to get is “2” for where the base_folder[‘CUP0001’], sub_folder[‘F’], image[‘B_FF_0.png’], image_type[‘B’], view[‘FF’], layer[‘2’]
The direct answer to the original question is:
If you need the maximum layer for each combination of the five fields, then you need a GROUP BY clause and you need to select the identifying columns too:
(Withdrawn:
)It looks suspiciously as if your table is not properly normalized; there is at least room to suppose that the combination of Base_Folder, Sub_Folder, Image controls the values of Image_Type and View. However, the code shown will work whether this comment is accurate or not.
It depends on the volume of data, and how you’re going to be managing the data. It also depends on which parts of the data are independent. I would be tempted to split the table into two, though an extreme view might go as far as five tables:
An alternative design for ImageDetails avoids the ID column and uses the alternate key as the primary key. It depends, in part, on whether anything else is going to reference the rows in this table.
However, it all depends on what you’re going to do and how the data is really organized (what the functional dependencies are in the table you show). The extreme view would use: one table for the base folders; then there’d be a sub-folder table with a base folder ID and the sub-folder name an its own ID field, and then there’d be an image file table with its own ID, a sub-folder ID, and the image name; then there’d be an image type table for each image type of each image file (with its own ID plus an FK); and so on. This is harder to insert rows into; and every select operation that needs to filter on, say, base folder and image type needs to join a lot of tables. That needn’t be slow, but it complicates the SQL.