分类表 t_class 里有 8个分类 字段是id name
公司表 corp 里有 id name class_id,其中class_id 外键是t_class表中的 id在corp表里取每种分类下的随即三条数据.mysql数据库
公司表 corp 里有 id name class_id,其中class_id 外键是t_class表中的 id在corp表里取每种分类下的随即三条数据.mysql数据库
调试欢乐多
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)
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)