日期:2014-05-18  浏览次数:20515 次

Sql循环查询(急),高手帮帮忙
我循环读取新闻,有两个表,类别表和新闻表
class: c_id,C_name

news: n_id,n_name,c_id

我现在的要求是读出类别名称和下面的20条新闻

我之前是这样写的

select c_id,c_name From class
  do while not rs.eof
 读出名字:rs("c_name")
  "select top 20 n_name from new where c_id="&rs("c_id")
  do while Not N_Rs.Eof
  读出新闻
  n_rs.movenext
  loop
  rs.movenext
  loop
这样写是最笨的 现在数据量大了 要多读好多条Sql 语句 数度很慢 有好点的查询方法吗??
在线等 急啊。。。。

 

------解决方案--------------------
SQL code
select a.* from 
(
  select c_name,n_name from class,news where class.c_id = news.c_id
) a 
where n_name in (select top 20 n_name from 
  (
     select c_name,n_name from class,news where class.c_id = news.c_id
  ) b where c_name=a.c_name ) 
order by a.c_name,a.n_name

------解决方案--------------------
--每個類別下的20條?

select a.C_name,b.n_name from class a,news b
where a.c_id=b.c_id
and b.n_id in (select top 20 n_id from news where c_id=a.c_id order by n_id desc)
order by a.c_name,b.n_id desc
------解决方案--------------------
SQL code
select n_id, n_name, C_name 
from (select * from news N where (select count(1) from news where c_id=N.c_id and n_id>N.n_id)<20) News
  join class on News.c_id=class.c_id
order by N.c_id,N.n_id desc