0

我有一个格式如下的表格:

 mysql> select pattern,trunks,cost from sms_prices where pattern=1;
 +---------+--------------+-------+
 | pattern | trunks       | cost  |
 +---------+--------------+-------+
 | 1       | Vitelity     | 0.099 |
 | 1       | Plivo        | 0.012 |
 | 1       | Twilio       | 0.012 |
 +---------+--------------+-------+
 3 rows in set (0.00 sec)

我的问题是:

考虑到该表还有另外 700 多个条目,其中 3-4 个条目用于相同的模式,我如何选择 DISTINCT(pattern) 按成本排序,ASC?

我试过这个:

 mysql> select DISTINCT pattern,cost,trunks from sms_prices where pattern=1 order by cost;
 +---------+-------+--------------+
 | pattern | cost  | trunks       |
 +---------+-------+--------------+
 | 1       | 0.012 | Plivo        |
 | 1       | 0.012 | Twilio       |
 | 1       | 0.099 | Vitelity     |
 +---------+-------+--------------+
 3 rows in set (0.00 sec)

 mysql>

但正如你所看到的,它仍然给了我相同的 3 个结果。

如果我只选择一个 DISTINCT 行,它会给我一个条目:

 mysql> select DISTINCT pattern from sms_prices where pattern=1 order by cost;
 +---------+
 | pattern |
 +---------+
 | 1       |
 +---------+
 1 row in set (0.00 sec)

但是我不知道这是哪个条目,所以结果是没用的。

请帮助查询将返回单个结果 per pattern,最小的cost

谢谢!

4

3 回答 3

1

也许这可能不是您想要的,但是:

SELECT pattern, cost, trunks 
FROM sms_prices 
WHERE cost = (select min(cost) from sms_prices where pattern = 1)
GROUP BY pattern;

问候

于 2013-06-13T20:15:51.617 回答
0

这行不通吗?

select pattern, min(cost) mincost
from sms_prices 
where whatever
group by pattern
于 2013-06-13T19:44:46.880 回答
0

SELECT pattern, cost, trunks FROM sms_prices AS OT WHERE cost = (select min(cost) from sms_prices as IT where OT.pattern=IT.pattern ) GROUP BY pattern;

于 2013-06-14T13:41:47.590 回答