简体   繁体   English

使用内部联接从查询返回最小值

[英]Return min value from query with inner join

I have two table: 我有两个表:

table POI: 表兴趣点:

NAME | NAME | VOTE 投票

Paris | 巴黎| rt_1 rt_1

Milan | 米兰| rt_2 rt_2

Rome | 罗马| rt_3 rt_3

... | ... | ... ...

table rtgitems: 表rtgitems:

ITEM | 项目| TOTALRATE 总计

rt_1 | rt_1 | 22 22

rt_2 | rt_2 | 3 3

rt_3 | rt_3 | 3 3

rt_4 | rt_4 | 5 5

... | ... | ... ...

I want the attribute NAME from first table with minimum value in TOTALRATE from second table. 我希望第一张表的属性NAME在第二张表的总值中具有最小值。 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. 每当您在sql中使用min或max kind或函数时,都应使用group by子句从表中获取实际输出。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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