两个记录限制在同一列时

时间:2014-09-21 11:22:26

标签: mysql orm kohana

我有专栏:

id | id_contract |价

我想从一份合约中选择限额为2的最低价格。

我使用kochana ORM。

感谢。

例如

1 | 1 | 100 *
2 | 1 | 500
3 | 1 | 300 *
4 | 1 | 900
5 | 2 | 1000
6 | 2 | 100 *
7 | 2 | 200 *
8 | 3 | 10000 *
    • 这就是我想要选择的内容。

1 个答案:

答案 0 :(得分:1)

您可以使用以下查询在MySQL中执行此操作:

select t.*
from table t
where (select count(*)
       from table t2
       where t2.id_contract = t.id_contract and
             t2.price <= t.price
      ) <= 2;