|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Selecting a filename column by MIN sortorder column rather than id
I'm setting up an image gallery. On the first page of the gallery's frontend I want want a repeat region that displays a list of album titles, album descriptions and the first thumbnail images associated with each given album as specified by the image's sortorder_img column (not the id). Unfortunately, I've been unable to write an SQL statement that returns the results I need and so I'm reaching out for some assistance.
Here are the tables: photos_alb Code:
id_alb int(11) auto_increment title_alb varchar(100) description_alb varchar(255) sortorder_alb int(11) photos_img Code:
id_img int(11) auto_increment idalb_img int(11) this is the foreign key to photos_alb filename_img varchar(100) caption_img varchar(255) sortorder_img int(11) incremented +1 on insert, remains unique when rearranged The SQL of the recordset: Code:
SELECT photos_alb.*, photos_img.* FROM photos_alb, photos_img WHERE photos_alb.id_alb=photos_img.idalb_img GROUP BY sortorder_alb ASC This allows me to create the repeat region with the album titles, album descriptions and thumbnails. However, the thumbnail filename_img is of course pulled from the first id_img encountered. I imagine that there's some way to incorporate the MIN() function into this to return the lowest numbered 'sortorder_img' and somehow use that to return the intended 'filename_img'. Can anyone provide any insight on how I might achieve this? Any help would be appreciated! P.S. If MIN(photos_img.filename_img) AS filename_img would reliably return the lowest numbered filename, I think I'd be good to go. However my image filenames are as follows: 1.jpg, 2.jpg, 3.jpg, etc. And the MIN() function doesn't appear to evaluate non numeric strings predictably. |
|
#2
|
||||
|
||||
|
Quote:
so you would have 1.jpg, 10.jpg, 11.jpg, 12.jpg, 2.jpg, 3.jpg, ... |
|
#3
|
||||
|
||||
|
Code:
SELECT photos_alb.*
, photos_img.*
FROM photos_alb
INNER
JOIN ( SELECT idalb_img
, MIN(sortorder_img) AS min_sortorder
FROM photos_img
GROUP
BY idalb_img ) as mins
ON mins.idalb_img = photos_alb.id_alb
INNER
JOIN photos_img
ON photos_img.idalb_img = photos_alb.id_alb
AND photos_img.sortorder_img = mins.min_sortorder
no offence, though, okay? your queries would just be a lot cleaner without that ![]() |
|
#4
|
||||
|
||||
|
Quote:
Ahh, I see. That does make perfect sense. Quote:
Huge thanks! I've been struggling with this for the last day and a half I'm ashamed to say. This query was the final holdup keeping me from finishing this gallery. Now, I'm going to celebrate by studying what you've so kindly contributed to learn from it. As for the naming convention I've used, I agree that it's redundant. I initially based this project off of a tutorial created by Adobe specifically for ADDT. I used the sample database that they provided and didn't simplify the field names as I should have. Again, thank you for your assistance. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Selecting a filename column by MIN sortorder column rather than id |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|