日期:2014-05-16  浏览次数:20769 次

百分急问一个mysql语句,请大家帮帮我~~
现在有产品表   product
pid       '产品id
cid       '公司id
name     '产品名

现在要求是这样的,求出,符合条件   name   like   '%aaa% '   的记录,但每个公司只能出一条

我现在是这样实现的,先求出每个公司符合条件的最大的记录,组成一个集合,作为子查询,然后再求出详细信息

如下

select   *   from   product
where   pid   in
  (
    select   max(pid)   as   maxid
    from   product
    where   name   like   '%aaa% '
    group   by   comid
    limit   0,20
  )
问题来了,我现在用的是mysql   4.1
他死活不让我在子查询中,用limit   0,20
如果把limit   0,20   放在外面的话,效率又极低

谁能帮帮我,谢谢

------解决方案--------------------
select *
from 表名 AS A
where [name] like '%aaa% '
and pid= (select top 1 pid from 表名 where cid=A.cid order by pid )
------解决方案--------------------
学习
------解决方案--------------------
你为什么要用limit呢?如果
select max(pid) as maxid
from product
where name like '%aaa% '
group by comid
的数目> 20的话,那后20条不是没了?
如果是前20条的话为什么不加排序语句呢