我有一张表machine
:
- id: int
- name: varchar (100)
- mark: varchar (100)
- price: double
包含数据:
1, keyboard, mark1, 10
2, mouse, mark2, 15
3, keyboard, mark3, 50
4, computer, mark4, 120
5, mouse, mark5, 90
我想显示具有按名称分组的机器列表,其中包含最后一个入门价格:
1(first), keyboard(first), mark1(first), 50(last)
2(first), mouse(first), mark2(first), 90(last)
4, computer, mark4, 120
我尝试了这个查询:
SELECT * FROM machine GROUP BY name
但我没有任何想法让我看到我的结果。
答案 0 :(得分:0)
SELECT name, MAX(price) FROM machine GROUP BY name
如果您还想选择标记,则需要对列进行分组或汇总
答案 1 :(得分:0)
有一个子查询m2,它返回每台机器的最高ID(即具有最新价格的行)。使用m2加入m1以获得整行:
select m1.*
from machine m1
join (select name, max(id) as id from machine group by name) m2
on m1.name = m2.name
and m1.id = m2.id
可以简化为:
select m1.*
from machine m1
join (select max(id) as id from machine group by name) m2
on m1.id = m2.id
答案 2 :(得分:0)
[END]
答案 3 :(得分:0)
应该做的伎俩
D:\Test\Info\TestProject.dll