mysql查询2个表 获取分类和分类列表
我只模拟一个 输出 3条商品信息的效果了.
否则 文字又要超长了
CREATE TABLE sp_classify ( cid INT, cname VARCHAR(10));INSERT INTO sp_classify VALUES(1, '笔记本');INSERT INTO sp_classify VALUES(2, '台式机');INSERT INTO sp_classify VALUES(3, '超级本');INSERT INTO sp_classify VALUES(4, '平板');CREATE TABLE sp_goods ( cid INT, gtitle VARCHAR(50));INSERT INTO sp_goods VALUES(1, '笔记本1');INSERT INTO sp_goods VALUES(1, '笔记本2');INSERT INTO sp_goods VALUES(1, '笔记本3');INSERT INTO sp_goods VALUES(1, '笔记本4');INSERT INTO sp_goods VALUES(1, '笔记本5');INSERT INTO sp_goods VALUES(2, '台式机1');INSERT INTO sp_goods VALUES(2, '台式机2');INSERT INTO sp_goods VALUES(2, '台式机3');INSERT INTO sp_goods VALUES(2, '台式机4');INSERT INTO sp_goods VALUES(2, '台式机5');INSERT INTO sp_goods VALUES(3, '超级本1');INSERT INTO sp_goods VALUES(3, '超级本2');INSERT INTO sp_goods VALUES(3, '超级本3');INSERT INTO sp_goods VALUES(3, '超级本4');INSERT INTO sp_goods VALUES(3, '超级本5');INSERT INTO sp_goods VALUES(4, '平板1');INSERT INTO sp_goods VALUES(4, '平板2');INSERT INTO sp_goods VALUES(4, '平板3');INSERT INTO sp_goods VALUES(4, '平板4');INSERT INTO sp_goods VALUES(4, '平板5');SELECT resultNameFROM(SELECT 0 as seqNo, cid, cname as resultName FROM sp_classifyUNION ALLSELECT CASE WHEN @cn != cid THEN @rownum:= 1 ELSE @rownum:= @rownum + 1 END as seqNo, @cn := cid AS cid, gtitle as resultName FROM sp_goods, (SELECT @rownum:=1) r, (SELECT @cn:=0) p) subQueryWHERE SeqNo