请大家帮我看看这条mysql语句该怎么写呢?
table1
id leixing
1 a
2 b
3 a
4 a
5 c
6 b
==============
table2
leiid leiname
a xxx
b yyy
c xxx
===============
以上分别有两个表table1和table2,我现在想实现的是,对于table1,每个leixing只查询id最大的那一条,并且该leixing字段在table2中对应的是leiid字段,要求它的leiname为xxx值,最后出来的结果按照id的降序排列,请大家帮我看看这条mysql语句该怎么写呢?
select id,leixing from table1 where leixing in (select leiid from table2 where leiname=’xxx’) group by leixing order by id desc
我这样写总是出错啊。
请大家帮我看看这条mysql语句该怎么写呢?
------解决方案--------------------select max(id),leixing from table1 where leixing in
(select leiid from table2 where leiname= 'xxx ') group by leixing order by id desc;