当前位置:编程学习 > C#/ASP.NET >>

求一SQL 语句.. 谢谢..

 求 一查询SQL 语句..
tb_Album AlbumID Name 
           1       我很你
           2      我逗你
tb_Novels AbID,  Mp3 ,    CreatTime
            1    001.Mp3  2010-12..
            2    001.Mp3   2010-12..
            1    002.Mp3    2011-1


查询后的结果是

我很你 002.mp3 +时间
我逗你 001.mp3 
 
 也就是更新后的 最后一个 MP3的值

     --------------------编程问答-------------------- --------------------编程问答--------------------
引用 1 楼 kevin87923 的回复:
没看懂

就是一本小说 有一个专辑名称  下面更新很多小说章节  我要取 更新了的 前10的小说 并获取出最后一个更新到哪
--------------------编程问答--------------------

--> 测试数据:[tb_Album]
if object_id('[tb_Album]') is not null drop table [tb_Album]
create table [tb_Album]([AlbumID] int,[Name] varchar(6))
insert [tb_Album]
select 1,'我很你' union all
select 2,'我逗你'
--> 测试数据:[tb_Novels]
if object_id('[tb_Novels]') is not null drop table [tb_Novels]
create table [tb_Novels]([AbID] int,[Mp3] varchar(7),[CreatTime] varchar(7))
insert [tb_Novels]
select 1,'001.Mp3','2010-12' union all
select 2,'001.Mp3','2010-12' union all
select 1,'002.Mp3','2011-1'

select * 
from [tb_Album] a ,[tb_Novels] b
where b.[AbID]=a.[AlbumID]
and not exists(select 1 from [tb_Novels] where b.[AbID]=[AbID] and b.CreatTime<CreatTime)


/*
AlbumID     Name   AbID        Mp3     CreatTime
----------- ------ ----------- ------- ---------
1           我很你    1           002.Mp3 2011-1
2           我逗你    2           001.Mp3 2010-12

(2 行受影响)

*/
drop table [tb_Album]
drop table [tb_Novels]
--------------------编程问答--------------------
引用 3 楼 jiangshun 的回复:
SQL code

--> 测试数据:[tb_Album]
if object_id('[tb_Album]') is not null drop table [tb_Album]
create table [tb_Album]([AlbumID] int,[Name] varchar(6))
insert [tb_Album]
select 1,'我很你' union all
se……


不逆的sql太帅了! --------------------编程问答--------------------
create table tb_Album (AlbumID int ,Name nvarchar(100))
create table tb_Novels(AbID int, Mp3 varchar(100), CreatTime datetime)
insert into tb_Album
select 1 ,'我很你'
union all select  2 ,'我逗你'
insert into tb_Novels 
select 1 ,'001.Mp3', '2010-12-01'
union all select  2 ,'001.Mp3', '2010-12-03'
union all select  1 ,'002.Mp3', '2010-12-12'

select Name,Mp3,CreatTime
from tb_Novels t1
inner join tb_Album t2
on t1.AbID=t2.AlbumID
where not exists(select null from tb_Novels where AbID=t1.AbID and CreatTime>t1.CreatTime)
--结果:
Name Mp3 CreatTime
我逗你 001.Mp3 2010-12-03 00:00:00.000
我很你 002.Mp3 2010-12-12 00:00:00.000
--------------------编程问答--------------------
引用 3 楼 jiangshun 的回复:
SQL code

--> 测试数据:[tb_Album]
if object_id('[tb_Album]') is not null drop table [tb_Album]
create table [tb_Album]([AlbumID] int,[Name] varchar(6))
insert [tb_Album]
select 1,'我很你' union all
select ……


搜索后 类别有重复 也就是AbID那重复 --------------------编程问答-------------------- 查询后..
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,