日期:2014-05-17  浏览次数:20486 次

看看这条语句有啥错
SELECT ctree_id, ctree_name,
  (SELECT COUNT(*)
  FROM jilu
  WHERE jilu_alldir LIKE '%|' + cast(ctree_id AS nvarchar) + '|%') AS ecount
FROM ctree
WHERE (ctree_reid = 1)

ctree表是一个目录树结构表
我想从jilu表中看看每个ctree条目在jilu表中有多少条数据,怎么获取出来的都是错误的记录数,都是jilu表的总记录数???

jilu表中有一个alldir字段 ,存储的数据代表这个条记录从属于多少个不同的目录结构id
存储形式 |1|2|3|4|5|6| 表示这条记录如果看1目录也能看到、2目录也能看到、3目录也能看到。。。。。



------解决方案--------------------
两个表没有关联

SELECT a.ctree_id, a.ctree_name,
(SELECT COUNT(*)
FROM jilu b
WHERE jilu_alldir LIKE '%|' + cast(a.ctree_id AS nvarchar) + '|%') AS ecount
FROM ctree a
WHERE (a.ctree_reid = 1)