简体   繁体   中英

Return min value from query with inner join

I have two table:

table POI:

NAME | VOTE

Paris | rt_1

Milan | rt_2

Rome | rt_3

... | ...

table rtgitems:

ITEM | TOTALRATE

rt_1 | 22

rt_2 | 3

rt_3 | 3

rt_4 | 5

... | ...

I want the attribute NAME from first table with minimum value in TOTALRATE from second table. Example: Milan, Rome.

I use this query:

SELECT POI.Name FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item WHERE POI.Vote = (SELECT MIN(rtgitems.totalrate) FROM rtgitems)

but don't work, I have empty result. How must I do? Thanks.

SELECT POI.Name, min(totalrate) FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item
GROUP BY POI.Name

When ever you are using min or max kind or function in your sql you should use group by clause to get the real output from the table.

SELECT POI.Name FROM POI INNER JOIN rtgitems ON POI.Vote=rtgitems.item where totalrate= (select min(totalrate) from rtgitems)
GROUP BY POI.Name 

hope it helps.

尝试SELECT POI.name FROM POI join rtgitems ON POI.vote=rtgitems.item where totalrate<=(SELECT totalrate from rtgitems order by totalrate desc limit 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM