問(wèn)題:
          在BBS中,content 和? review 兩張表,其中content 記錄的是 帖子信息(topic_id,topic_title,topic_content等字段),review 表中記錄的是回帖的信息,(review_id,topic_id等字段)在 求一SQL語(yǔ)句,其中topic_id和review_id都是自動(dòng)增加,現(xiàn)在要求查詢出有最新回復(fù)的帖子的信息,同時(shí)要求有多個(gè)回復(fù)的一個(gè)帖子的主題只能顯示一次,能用一個(gè)SQL語(yǔ)句搞定嗎?數(shù)據(jù)庫(kù)是MYSQL 求高手指點(diǎn)下

          回答:
          select t.id,t.title,t.content,r.newposttime from topic as t inner join (select max(posttime) as newposttime, topicid from review group by topicid) as r on t.id=r.topicid
          ?
          //測(cè)試
          create table topic(
          ? id int primary key identity(1,1),
          ? title varchar(128),
          ? content text
          )

          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')
          insert into topic(title,content) values('title1','content1')


          create table review(
          ? id int primary key identity(1,1),
          ? topicid int references topic(id),
          ? content text,
          ? posttime datetime default getdate()
          )

          insert into review(topicid,content) values(1,'reice1')
          insert into review(topicid,content) values(1,'reice1')
          insert into review(topicid,content) values(2,'reice1')
          insert into review(topicid,content) values(2,'reice1')
          insert into review(topicid,content) values(2,'reice1')
          insert into review(topicid,content) values(3,'reice1')
          insert into review(topicid,content) values(3,'reice1')
          insert into review(topicid,content) values(3,'reice1')
          insert into review(topicid,content) values(3,'reice1')
          insert into review(topicid,content) values(4,'reice1')

          select t.id,t.title,t.content,r.newposttime from topic as t inner join (select max(posttime) as newposttime, topicid from review group by topicid) as r on t.id=r.topicid

          這些是測(cè)試數(shù)據(jù),sqlserver2000測(cè)試通過(guò)

          posted on 2006-09-28 18:49 pear 閱讀(451) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 心得體會(huì)
           
          主站蜘蛛池模板: 杭州市| 彰化市| 辽阳市| 庐江县| 东乌珠穆沁旗| 富裕县| 石城县| 万载县| 察雅县| 慈利县| 高密市| 莒南县| 泗洪县| 法库县| 中山市| 建德市| 礼泉县| 金乡县| 苏州市| 普定县| 卢氏县| 桃园市| 蒙城县| 枣阳市| 阿克陶县| 蒙山县| 永清县| 昌乐县| 长宁县| 新和县| 客服| 房山区| 五台县| 当涂县| 南召县| 奉化市| 辽阳县| 大荔县| 从化市| 宜都市| 淮安市|