我的结果设置似乎很好,除了我得到了我想避免的其他重复结果,就是这种情况:
我正在使用两张桌子:
顺序
id|companyname|routeid|orderdate
1 |company1 | 23dsa |
2 |company1 | 23dsa | 2011-04-28
3 |company1 | 23dsa | 2011-04-05
4 |company2 | 24gcc | 2011-22-05
车辆
id| vehiclename | routeid
1 | van1 | 23dsa
2 | van2 | 24gcc
这是我的查询1:
select t1.companyname, max(t1.orderdate) as orderdate
from order t1, vehicle t2 where t1.routeid=t2.routeid
group by t1.companyname, t1.orderdate
order by t1.id
这是查询2:
select t1.companyname, max(t1.orderdate) as orderdate
from order t1, vehicle t2 where t1.routeid=t2.routeid
group by t1.companyname, t1.orderdate having count(1) = 1
order by t1.id
这是我的结果:
companyname | orderdate
company1 |
company1 | 2011-04-28
company1 | 2011-04-05
company2 | 2011-22-05
我希望它是这样的:
companyname | orderdate
company1 | 2011-04-28
company2 | 2011-22-05
我失踪不是空的:
select t1.companyname, max(t1.orderdate) as orderdate
from order t1, vehicle t2 where t1.orderdate is not null and t1.routeid=t2.routeid
group by t1.companyname, t1.orderdate
order by t1.id
答案 0 :(得分:0)
select t1.companyname, max(t1.orderdate) as orderdate
from order t1, vehicle t2 where t1.routeid=t2.routeid
group by t1.companyname
order by t1.id
无需按顺序分组,因为您只选择一个。
答案 1 :(得分:0)
怎么样:
SELECT
companyname, MAX(orderdate) AS orderdate
FROM
`order`
GROUP BY
companyname
ORDER BY
id
如果你需要那些车辆:
SELECT
companyname, MAX(orderdate) AS orderdate
FROM
`order` o, vehicle v
WHERE
o.routeid=v.routeid
GROUP BY
companyname
ORDER BY
o.id