我想你需要加入:
select p.*
from product p join
(select p.name, max(len(info)) as maxinfolen
from product p
group by p.name
) t
on p.name = t.name and
len(p.info) = t.maxinfolen如果在info中重复最大长度,这将返回重复项。要消除重复,您可以在外面进行分组:
select p.name, max(info)
from product p join
(select p.name, max(len(info)) as maxinfolen
from product p
group by p.name
) t
on p.name = t.name and
len(p.info) = t.maxinfolen
group by p.name如果您每次都尝试选择相同的ID,则可以使用相同的想法:
select p.*
from product p join
(select p.name, min(id) as minid
from product p join
(select p.name, max(len(info)) as maxinfolen
from product p
group by p.name
) t
on p.name = t.name and
len(p.info) = t.maxinfolen
group by p.name
) t
on p.name = t.name and
p.id = t.minid