Simple MySQL query question

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 image identifier, and then select from the bin table the bin identifier that it matches. For example, for picture.id = 59 (highest), I want bins.id = 3. Can someone help me with this request? Sort of

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

doesn't seem to work. Thank!

+3
source share
3 answers
SELECT id 
FROM bins
WHERE min < (Select Max(id) from Pictures) 
  AND max > (Select Max(id) from Pictures) 

Hope this helps

Max

+2
source

try it

   Select id From Bins
   Where (Select Max(id) From pictures)
       Between Min and Max
+1
source

bin (bin.min bin.max) , , bin max :

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

<= => - (, picture.id = 41 ).

:

SELECT MAX( bins.id )
FROM   bins
JOIN   pictures
ON     pictures.id BETWEEN bins.min AND bins.max

This will break if the limits of your bunker are not sorted with the bin identifier.

0
source

Source: https://habr.com/ru/post/1731631/


All Articles