查看qq空间说说及评论,设置相关表结构
id:主鍵、無意義
owner:說說的主人
sendtime:發(fā)布時間
content:說說內(nèi)容
-- -- 創(chuàng)建說說表
-- create table saysay(
-- id int unsigned auto_increment primary key,
-- owner varchar(20) not null,
-- sendtime datetime,
-- content text
-- );
-- insert into saysay (owner,sendtime,content) values ('張三','2016-03-15 15:05:10','今天天氣有點(diǎn)涼');
-- insert into saysay (owner,sendtime,content) values ('王梅','2016-03-15 15:10:10','今天天氣真涼');
評論表(comments)
id:主鍵、無意義(id 為0 代表一級評論)
owner:該評論的發(fā)出者
toname: 該評論是回復(fù)哪個人的
sendtime:評論的時間
content:評論的內(nèi)容
ssid:是哪條說說的下面的評論
pid:一級評論的id
-- -- 創(chuàng)建評論表
-- create table comments (
-- id int unsigned auto_increment primary key,
-- owner varchar(20) not null,
-- toname varchar(20) not null,
-- sendtime datetime not null,
-- content text not null,
-- ssid int unsigned not null,
-- pid int,
-- foreign key (ssid) references saysay(id),
-- foreign key (pid) references comments(id),
-- foreign key (toname) references comments(owner)
-- );
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','張三','2016-03-15 15:08:10','就是,聽說有雷陣雨',1,0);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('王五','李四','2016-03-15 15:09:30','哎呦,真的嗎?沒帶傘咋辦?',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('張三','李四','2016-03-15 15:10:10','好煩,又要下雨',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','王五','2016-03-15 15:16:00','那只能自求多福了',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','張三','2016-03-15 15:16:30','確實(shí)挺煩人的',1,1);
--?
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王五','2016-03-15 15:10:00','凍成煞筆了吧!',1,0);
-- insert into comments (owner,sendtime,content,ssid,pid) values ('張三','2016-03-15 15:13:10','你才是煞筆呢',1,16);
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王五','2016-03-15 15:15:30','煞筆說誰呢?',1,16);
--?
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王紅','2016-03-15 15:17:30','穿厚點(diǎn)',1,0);
java代碼邏輯:
1、獲取說說內(nèi)容之后,得到說說的id
2、根據(jù)說說的id,獲取該說說下的評論(按照發(fā)布時間遞增的順序查詢),并且輸出一級評論
3、在每輸出一條一級評論之后,獲取該一級評論的id,獲取該一級評論下的所有評論即可。
——————————————————————————————————————————————————————
CREATE DATABASE qzone;
#用戶表(用戶,姓名)
CREATE TABLE quser(
uid INT PRIMARY KEY AUTO_INCREMENT,
uno INT UNIQUE ?NOT NULL,
uname VARCHAR(10) NOT NULL
);
INSERT INTO quser(uno,uname)
VALUES (1,'桃子'),
(2,'祁玉'),
(3,'歡姐'),
(4,'夢雅'),
(5,'小諾');
SELECT * FROM quser;
#說說表(說說編號,用戶,內(nèi)容,時間)
CREATE TABLE topic(
tid INT PRIMARY KEY AUTO_INCREMENT,
tno INT UNIQUE NOT NULL,
tuno INT NOT NULL,
tcontent VARCHAR(100) NOT NULL,
tdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (tuno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO topic(tno,tuno,tcontent)
VALUES (1,1,'舉杯邀明月,對影成三人'),
(2,1,'假如生活欺騙了你,不要悲傷,不要著急'),
(3,2,'月是故鄉(xiāng)明'),
(4,3,'今天吃的好飽呀'),
(5,4,'我已經(jīng)控制不住體內(nèi)的洪荒之力了!');
SELECT * FROM topic;
#評論表(評論編號,說說編號,用戶,內(nèi)容,時間)
CREATE TABLE discuss(
did INT PRIMARY KEY AUTO_INCREMENT,
dno INT UNIQUE NOT NULL,
dtno INT NOT NULL,
duno INT NOT NULL,
dcontent VARCHAR(100) NOT NULL,
ddate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (dtno) REFERENCES topic (tno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (duno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO discuss(dno,dtno,duno,dcontent)
VALUES(1,2,5,'普希金的詩,我喜歡!'),
(2,4,1,'今天吃啥了呀歡姐'),
(3,3,4,'祁玉又文藝了');
SELECT * FROM discuss;
#回復(fù)表replay(回復(fù)編號,評論編號,被回復(fù)用戶,用戶,內(nèi)容,時間)
CREATE TABLE replay(
rid ?INT PRIMARY KEY AUTO_INCREMENT,
rno INT UNIQUE NOT NULL,
rdno INT NOT NULL,
runum INT NOT NULL,
runo INT NOT NULL,
rcontent ?VARCHAR(100) NOT NULL,
rdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (rdno) REFERENCES discuss (dno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (runum) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (runo) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO replay(rno,rdno,runum,runo,rcontent)
VALUES (1,1,5,1,'小諾你知道的太多了,你是alphago的親戚吧'),
(2,1,1,5,'對呀桃子,我親戚老厲害了'),
(3,1,5,1,'給你點(diǎn)顏色就開染坊。。。'),
(4,2,1,3,'豆?jié){油條'),
(5,3,4,2,'總要有詩和遠(yuǎn)方');
SELECT * FROM replay;
#點(diǎn)贊表admire(點(diǎn)贊編號,說說編號,用戶編號,時間)
CREATE TABLE admire(
aid INT PRIMARY KEY AUTO_INCREMENT,
ano INT UNIQUE NOT NULL,
atno INT NOT NULL,
auno INT NOT NULL,
adate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (atno) REFERENCES topic (tno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (auno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO admire(ano,atno,auno)
VALUES(1,1,1),
(2,1,2),
(3,1,3),
(4,3,2),
(5,4,3);
SELECT * FROM admire;
#查詢用戶說說
SELECT uname '用戶',tcontent '說說',tdate '時間'
FROM topic,quser
WHERE quser.`uno`=topic.`tuno`?
#創(chuàng)建說說視圖
CREATE VIEW shuo
AS
SELECT uno,uname,tno,tcontent,tdate
FROM topic,quser
WHERE quser.`uno`=topic.`tuno` ;
SELECT * FROM shuo;
#查詢評論
SELECT uname,tcontent
FROM discuss,topic
WHERE quser.`uno`=discuss.`dtno` AND?
總結(jié)
以上是生活随笔為你收集整理的查看qq空间说说及评论,设置相关表结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5分钟学会Cron表达式
- 下一篇: 平面设计新手怎么构思设计?有什么技巧!