Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL MIN Function with where clause

This is my Project Table

 Project Table
JNo Name    City
J1  Proj1   London
J2  Proj2   Paris
J3  Proj3   Athens
J4  Proj4   India

And this is my shipment table

Shipment
SNo PNo JNo Qty
S1  P1  J1  50
S1  P1  J2  90
S1  P2  J1  40
S1  P3  J3  20
S2  P1  J3  110
S2  P2  J2  30
S2  P4  J3  10
S2  P3  J1  100
S3  P1  J3  80
S3  P4  J2  70
S3  P4  J2  70
S4  P1  J3  20
S4  P2  J1  60

I want to name of the project having minimum quantity supplied.

I tried. But its return only minimum qty value this is my code

select min(qty) from shipment where jno IN(select jno from project)
like image 632
KKK Avatar asked Mar 08 '12 12:03

KKK


2 Answers

SELECT p.name 
FROM Project p, Shipment s
WHERE s.JNo = p.JNo
  AND s.Qty in (SELECT MIN(qty) FROM shipment)
like image 66
Andreas Avatar answered Oct 04 '22 04:10

Andreas


Without using MIN:


    SELECT p.Name, s.Qty
    FROM `project` p
    INNER JOIN `shipment` s ON `p`.`jno` = `s`.`jno`
    ORDER BY `s`.`qty` ASC
    LIMIT 1

like image 43
Sudhir Bastakoti Avatar answered Oct 04 '22 03:10

Sudhir Bastakoti