public partial class controls_Bulletin : System.Web.UI.UserControl
{
private SqlCommand Command;
private SqlConnection Connection;
private SqlDataAdapter DataAdapter;
private DataTable NewsTable;
private XmlDocument XmlDoc;
//protected Panel NoPicturePan;
private void DataLoad()
{
this.EnableViewState = false;
{
this.XmlDoc = new XmlDocument();
this.XmlDoc.Load(base.Request.PhysicalApplicationPath + “Config/AppConfig.config”);
byte num1 = Convert.ToByte(this.XmlDoc.GetElementsByTagName(“TopImgNum”).Item(0).Attributes[“value”].Value);
byte num2 = Convert.ToByte(this.XmlDoc.GetElementsByTagName(“TopNewsNum”).Item(0).Attributes[“value”].Value);
{
this.NewsTable = new DataTable();
this.Connection = new SqlConnection(DataProvider.ConnectionString);
this.Command = new SqlCommand();
this.Command.Connection = this.Connection;
this.Command.CommandText = “Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc “;
this.DataAdapter = new SqlDataAdapter();
this.DataAdapter.SelectCommand = this.Command;
this.DataAdapter.Fill(this.NewsTable);
this.Command.Dispose();
this.DataAdapter.Dispose();
this.Connection.Dispose();
this.NewsTable.Columns.Add(“ClassName”, typeof(string));
for (int num3 = 0; num3 < this.NewsTable.Rows.Count; num3++)
{
for (int num4 = 0; num4 < NavigateTree.InfinityTree.Rows.Count; num4++)
{
if (Convert.ToInt32(this.NewsTable.Rows[num3][“Classid”]) == Convert.ToInt32(NavigateTree.InfinityTree.Rows[num4][“Classid”]))
{
this.NewsTable.Rows[num3][“ClassName”] = NavigateTree.InfinityTree.Rows[num4][“ClassName”];
}
}
}
this.ArticleRpt.DataSource = this.NewsTable;
this.ArticleRpt.DataBind();
this.NewsTable.Dispose();
}
}
}
protected static string Format(string instr, int count, bool hl)
{
instr = OutPut.OutString(instr, count);
instr = OutPut.HeighLight(instr, hl);
return instr;
}
protected static string Format(string instr, int count, DateTime dt, bool hl)
{
instr = OutPut.OutString(instr, count);
instr = OutPut.HeighLight(instr, hl);
instr = instr + OutPut.GetNewImage(dt);
return instr;
}
protected void Page_Load(object sender, EventArgs e)
{
this.DataLoad();
}
}
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc
=>
Select Top 2 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc
引用 楼主 qq_21021037 的回复:
public partial class controls_Bulletin : System.Web.UI.UserControl
{
private SqlCommand Command;
private SqlConnection Connection;
private SqlDataAdapter DataAdapter;
private DataTable NewsTable;
private XmlDocument XmlDoc;
//protected Panel NoPicturePan;
private void DataLoad()
{
this.EnableViewState = false;
{
this.XmlDoc = new XmlDocument();
this.XmlDoc.Load(base.Request.PhysicalApplicationPath + “Config/AppConfig.config”);
byte num1 = Convert.ToByte(this.XmlDoc.GetElementsByTagName(“TopImgNum”).Item(0).Attributes[“value”].Value);
byte num2 = Convert.ToByte(this.XmlDoc.GetElementsByTagName(“TopNewsNum”).Item(0).Attributes[“value”].Value);
{
this.NewsTable = new DataTable();
this.Connection = new SqlConnection(DataProvider.ConnectionString);
this.Command = new SqlCommand();
this.Command.Connection = this.Connection;
this.Command.CommandText = “Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc “;
this.DataAdapter = new SqlDataAdapter();
this.DataAdapter.SelectCommand = this.Command;
this.DataAdapter.Fill(this.NewsTable);
this.Command.Dispose();
this.DataAdapter.Dispose();
this.Connection.Dispose();
this.NewsTable.Columns.Add(“ClassName”, typeof(string));
for (int num3 = 0; num3 < this.NewsTable.Rows.Count; num3++)
{
for (int num4 = 0; num4 < NavigateTree.InfinityTree.Rows.Count; num4++)
{
if (Convert.ToInt32(this.NewsTable.Rows[num3][“Classid”]) == Convert.ToInt32(NavigateTree.InfinityTree.Rows[num4][“Classid”]))
{
this.NewsTable.Rows[num3][“ClassName”] = NavigateTree.InfinityTree.Rows[num4][“ClassName”];
}
}
}
this.ArticleRpt.DataSource = this.NewsTable;
this.ArticleRpt.DataBind();
this.NewsTable.Dispose();
}
}
}
protected static string Format(string instr, int count, bool hl)
{
instr = OutPut.OutString(instr, count);
instr = OutPut.HeighLight(instr, hl);
return instr;
}
protected static string Format(string instr, int count, DateTime dt, bool hl)
{
instr = OutPut.OutString(instr, count);
instr = OutPut.HeighLight(instr, hl);
instr = instr + OutPut.GetNewImage(dt);
return instr;
}
protected void Page_Load(object sender, EventArgs e)
{
this.DataLoad();
}
}
你说的这个我知道,就是要每一类我提取一条先试试。想提取classid=24和4的数据,但是每次都只显示classid=4的那条
引用 1 楼 guwei4037 的回复:
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc
=>
Select Top 2 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid in (24,4) Order By sortid desc,addtime Desc
你说的这个我知道,就是要每一类我提取一条先试试。想提取classid=24和4的数据,但是每次都只显示classid=4的那条
Select Top 1 Articleid
你都top1了,当然只有一个,去掉限制就行,你这个貌似上个帖子里就有人回答你了
引用 4 楼 starfd 的回复:
Select Top 1 Articleid
你都top1了,当然只有一个,去掉限制就行,你这个貌似上个帖子里就有人回答你了
嗯,之前发了个帖子没解决。自己看了看还是没想明白,我要实现的是提取两个板块的前两条内容,分别是classid=4,24应该怎么写呢?
Select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article
INNER JOIN
(SELECT MIN(Articleid) AS ID FROM Article
WHERE Classid in (24,4)
GROUP BY Classid)t ON Article.Articleid=t.ID
Order By sortid desc,addtime DESC
SQL改成这样子,最小的两个应该也就是Articleid最小的两个吧?
Select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article
INNER JOIN
(SELECT MIN(Articleid) AS ID,Classid FROM Article
WHERE Classid in (24,4)
GROUP BY Classid)t ON Article.Articleid=t.ID
Order By sortid desc,addtime DESC
忘记了Classid ,结果就不一样了
5分
where Classid in (24,4)
你是想取 Classid 等于 24 和 4 的各一条是这样的吗?
那么需要使用联合查询
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid = 24
union
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid = 4
Order By sortid desc,addtime Desc
引用 8 楼 xuzuning 的回复:
where Classid in (24,4)
你是想取 Classid 等于 24 和 4 的各一条是这样的吗?
那么需要使用联合查询
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid = 24
union
Select Top 1 Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article where Classid = 4
Order By sortid desc,addtime Desc
试了试这个结果报错:如果该语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中
引用 7 楼 starfd 的回复:
Select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop From Article
INNER JOIN
(SELECT MIN(Articleid) AS ID,Classid FROM Article
WHERE Classid in (24,4)
GROUP BY Classid)t ON Article.Articleid=t.ID
Order By sortid desc,addtime DESC
忘记了Classid ,结果就不一样了
报错: Classid不明确
5分
选择top 1就是指显示查询出来的第一条数据而已的!
你用的是什么数据库?
都是 order 只能出现在最后,你的怎么是必须出现在子句中
即如此你就在第一的 select 中也加上 order 就是了
5分
Select?Articleid,Article.Classid,Title,AddTime,HighLight,Hits,IndexTop?From?Article?
INNER?JOIN?
?(SELECT?MIN(Articleid)?AS?ID,Classid??FROM?Article
?WHERE?Classid?in?(24,4)?
?GROUP?BY?Classid)t?ON?Article.Articleid=t.ID
?Order?By?sortid?desc,addtime?DESC
这些小错误自己还搞不定啊……
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1
已经测试过了,可行,而且可以随意添加Classid值的数量
引用 14 楼 buhuiba139 的回复:
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1
已经测试过了,可行,而且可以随意添加Classid值的数量
嗯,可以用。我要是想控制classid=24,4的显示顺序该怎么办呢,比如说我想先显示24的那行在现实4的那行。还有,每一个里面我想提取两条数据,为什么我改成t.number limit 0,2报错呢
10分
引用 15 楼 qq_21021037 的回复:
Quote: 引用 14 楼 buhuiba139 的回复:
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1
已经测试过了,可行,而且可以随意添加Classid值的数量
嗯,可以用。我要是想控制classid=24,4的显示顺序该怎么办呢,比如说我想先显示24的那行在现实4的那行。还有,每一个里面我想提取两条数据,为什么我改成t.number limit 0,2报错呢
你第一个问题控制显示顺序,在后面加order by
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1 order by Classid desc
第二个问题,想每个里面提取两条数据
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num<=2 order by Classid desc
引用 16 楼 buhuiba139 的回复:
Quote: 引用 15 楼 qq_21021037 的回复:
Quote: 引用 14 楼 buhuiba139 的回复:
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1
已经测试过了,可行,而且可以随意添加Classid值的数量
嗯,可以用。我要是想控制classid=24,4的显示顺序该怎么办呢,比如说我想先显示24的那行在现实4的那行。还有,每一个里面我想提取两条数据,为什么我改成t.number limit 0,2报错呢
你第一个问题控制显示顺序,在后面加order by
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num=1 order by Classid desc
第二个问题,想每个里面提取两条数据
select Articleid,Classid,Title,AddTime,HighLight,Hits,IndexTop from (
select ROW_NUMBER() over(partition by classid order by sortid desc,AddTime desc) num,* from Article where Classid in (24,4)) t
where t.num<=2 order by Classid desc
谢谢了。我先试试吧,排序估计挺麻烦的,我其实是要提取classid=24,4,26的数据,我先试试看能不能改数据库里面的id吧