分类表 t_class 里有 8个分类 字段是id name
公司表 corp 里有 id name class_id,其中class_id 外键是t_class表中的 id在corp表里取每种分类下的随即三条数据.mysql数据库

解决方案 »

  1.   

    参考:
    SELECT a.sn,a.states,a.tableid FROM tt a 
    WHERE 2>=( 
    SELECT COUNT(*) FROM tt b 
    WHERE a.sn=b.sn and a.id>=b.id)
      

  2.   

    随机取则复杂复杂,用程序或者存储过程实现比较方便。如果只有这8个分类 则可以用 union all 来实现(select * from corp where class_id=1 order by rand() limit 3)
    union all
    (select * from corp where class_id=2 order by rand() limit 3)
    union all
    (select * from corp where class_id=3 order by rand() limit 3)
    union all
    ..
    union all
    (select * from corp where class_id=8 order by rand() limit 3)