Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple MySQL query question

Tags:

mysql

bins
----
id      min      max
1       1        20
2       21       40
3       41       60

pictures
--------
id
3
11
59

Basically, I want to select the highest picture Id and then select from the bin table the bin id it matches. For example, for a pictures.id = 59 (highest), I want bins.id = 3. Can anyone help me with such a query? Something like

SELECT bins.id AS id
FROM bins
    JOIN pictures.id 
    ON bins.min < MAX(pictures.id)
        AND bins.max > MAX(pictures.id)

doesn't appear to work. Thanks!

like image 590
axsuul Avatar asked May 24 '26 14:05

axsuul


1 Answers

SELECT id 
FROM bins
WHERE min < (Select Max(id) from Pictures) 
  AND max > (Select Max(id) from Pictures) 

Hope it helps

Max

like image 112
Massimo Fazzolari Avatar answered May 27 '26 05:05

Massimo Fazzolari