求一段查询代码有一张表IDtypecodenameparentID1大类1水泥null2小类1速凝水泥13小类2慢凝水泥14品牌1feife
求一段查询代码··
有一张表···
··ID··type··code··name··parentID··
··1··大类···1···水泥···null···
··2··小类···1··速凝水泥···1····
··3··小类···2··慢凝水泥···1····
··4··品牌···1··feifei····2····
··5··品牌···2···nini····2····
在一张表里面的类别有 大类,小类和品牌···parentID是标示···就是在 大类:水泥 下面有两种小类:速凝水泥和慢凝水泥···而速凝水泥又有两个品牌····parentID指向的是 上级目录的ID···
··········
求一句代码
找出大类代码为1 小类代码为1 的品牌····
要求 显示 大类code 大类name 小类code 小类name 品牌code 品牌name··
[解决办法]
根据楼主给出的数据,找出大类代码为1 小类代码为1 的品牌无符合的记录,因此改为找出大类代码为1 小类代码为2 的品牌
- SQL code
CREATE TABLE t1( id INT, style VARCHAR(10), code INT, name VARCHAR(50), pid INT)INSERT INTO t1SELECT 1,'大类',1,'水泥',NULL UNION ALLSELECT 2,'小类',1,'速凝水泥',1 UNION ALLSELECT 3,'小类',2,'慢凝水泥',1 UNION ALLSELECT 4,'品牌',1,'feifei',2 UNION ALLSELECT 5,'品牌',2,'nini',2SELECT * FROM t1SELECT a.code AS [大类code],a.NAME AS [大类name],b.code AS [小类code],b.NAME AS [小类name],c.code AS [品牌code],c.NAME AS [品牌name]FROM t1 AS a WITH(NOLOCK) INNER JOIN t1 AS b ON b.pid=a.code AND a.pid IS NULL AND b.code=2 INNER JOINt1 AS c WITH(NOLOCK) ON c.pid=b.code AND c.style='品牌'-------------------------------大类code 大类name 小类code 小类name 品牌code 品牌name1 水泥 2 慢凝水泥 1 feifei1 水泥 2 慢凝水泥 2 nini
