两个表
1、新闻栏目表db_Column
Id | ParId | ColumnName |
1 | 0 | 终极父栏目1 |
2 | 0 | 终极父栏目2 |
3 | 1 | 二级栏目1 |
4 | 1 | 二级栏目2 |
5 | 2 | 二级栏目3 |
2、新闻表db_News
Id | ColumnId | IsCheck | ....... | ....... |
1 | 3 | 1 | ||
2 | 3 | 1 | ||
3 | 4 | 0 | ||
4 | 4 | 1 | ||
5 | 4 | 1 | ||
6 | 5 | 1 |
参数为终极父栏目的Id,查询出新闻表中该终极父栏目下的,每个二级子栏目下的,第一篇IsCheck=1的新闻,要求查出新闻所有字段和对应栏目名称
SELECT n.*, c.ColumnName
FROM (db_News n INNER JOIN db_Column c ON n.ColumnId = c.Id)
WHERE (n.Id IN
(SELECT MIN(Id) FROM
(SELECT Id, ColumnId FROM db_News WHERE (IsCheck= 1) ) GROUP BY ColumnId))
AND
(n.ColumnId IN (SELECT Id FROM db_Column WHERE (ParId =参数)))
本文出自 “9110765” 博客,请务必保留此出处http://9120765.blog.51cto.com/9110765/1607742
原文地址:http://9120765.blog.51cto.com/9110765/1607742