单个查询Mysql中的多个条件?

时间:2014-07-23 10:29:20

标签: mysql

表名:Price

City        Gold_Rate   Silver_Rate Date

Mumbai      3000        60      13-07-2014
Delhi       4000        50      14-04-2014
Bangalore   1400        40      16-06-2014
Mumbai      1500        58      18-09-2014
Mumbai      2500        54      19-08-2014
Delhi       1800        60      01-10-2014
Bangalore   1700        44      02-03-2014  

预期输出将是;

City        Gold_Rate   Silver_Rate Date

Mumbai      1500        58      18-09-2014
Delhi       1800        60      01-10-2014
Bangalore   1400        40      16-06-2014  

我需要查询才能获得此输出

任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

select City,Gold_Rate,Silver_Rate,max(Date) from Price group by City

尝试此查询。

答案 1 :(得分:0)

在我看来,您希望在最小Gold_Rates的基础上获取结果,因此查询

select City,min(Gold_Rate),Silver_rate,Date from Price  group by City;

如果您想根据最新日期获取结果,请尝试使用

select City,Gold_Rate,Silver_Rate,max(Date) from Price group by City;

根据您在评论中提到的要求,更确切地说,您将使用此 Select city, gold_rate, silver_rate,date from price where Date in (select max(Date) from price group by city);

答案 2 :(得分:0)

select * from price where gold_rate = (select min(gold_rate) from price p where p.city = price.city);

或者你可以使用内连接

select * from price inner join (
    select min(gold_rate) gold_rate, city
    from price
    group by city
  ) as ip on ip.city = price.city and ip.gold_rate = price.gold_rate

两者都会为您提供所需的输出。 美好的一天