mysql - SQL Conditional Logic to select the 'most appropriate' record -
i simplified entire system shows enough particular question. i'm storing in table information 1 photo has been scaled down several times (maintain aspect ratio). i'm trying devise 1 query table can appropriate photo.
what mean appropriate is
if request 705px width largest 1 returned in case 700x394
if request 50px width smallest 1 returned in case 150x84
sample data photo resized dimensions
700x394 400x200 300x169 150xx84
more information
- the images in database proportionally resized down using reference edge.
- the reference edge longest edge means if receive portrait photo gets resized down proportionally *x700, *x400, *x300, *x150.
- if image landscape gets resized down same way 700x*, 400x*, 300x*, 150x*
this query partially works 0 maximum edge or lower. doesn't work if request dimension that's on maximum width.
select * images width >= 750 order width asc limit 1
i have working trick change logic based on portrait or landscape
ifnull(((width >= height , width >= $length ) or (width <= height , height >= $length )), true)
i have sql fiddle have question/problem outlined available here
any insight appreciated. understand read in data , logic in back-end language such php on high demand data pipe , want minimize overhead.
fiddle: http://sqlfiddle.com/#!9/91ca9/2
try this
select * images order abs(width-750) asc limit 1
using absolute value of width minus requested dimension return nearest image size. i'm sure adapt need check height also
Comments
Post a Comment