用户、留言、评论表的查询以及展示

一、用户表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `uuid` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'ID',
  `username` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '账号',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '密码',
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名',
  `phone` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '电话',
  `mail` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '邮箱',
  `sex` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '性别',
  `number` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '学号',
  `state` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT 'F' COMMENT '是否校验短信',
  `able` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT 'T' COMMENT '是否启用'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'abc', '7', '第一人', '1888888888', '[email protected]', '男', '201810010', 'F', 'T');
INSERT INTO `user` VALUES ('2', 'def', '7', '第二人', '1888888888', '[email protected]', '男', '201810012', 'F', 'T');
INSERT INTO `user` VALUES ('3', 'fng', '7', '第三人', '181888888888', '[email protected]', '男', '201810013', 'F', 'T');

SET FOREIGN_KEY_CHECKS = 1;


二、留言表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for online
-- ----------------------------
DROP TABLE IF EXISTS `online`;
CREATE TABLE `online`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `uid` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '留言人id',
  `content` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '留言内容',
  `creatime` datetime NULL DEFAULT NULL COMMENT '留言时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;

-- ----------------------------
-- Records of online
-- ----------------------------
INSERT INTO `online` VALUES (1, '1', '留的这是一条留言', '2018-04-02 20:58:32');
INSERT INTO `online` VALUES (2, '2', '留的这是二条留言', '2018-04-27 15:36:13');

SET FOREIGN_KEY_CHECKS = 1;


三、评论表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for reply
-- ----------------------------
DROP TABLE IF EXISTS `reply`;
CREATE TABLE `reply`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `cid` int(11) NULL DEFAULT NULL COMMENT '留言id',
  `uid` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '回复人id',
  `recontent` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '回复内容',
  `retime` int(11) NULL DEFAULT NULL COMMENT '回复时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;

-- ----------------------------
-- Records of reply
-- ----------------------------
INSERT INTO `reply` VALUES (1, 1, '2', '第二人回复第1条留言', NULL);
INSERT INTO `reply` VALUES (2, 2, '2', '第二人回复第2条留言', NULL);
INSERT INTO `reply` VALUES (3, 1, '1', '第一人回复第1条留言', NULL);
INSERT INTO `reply` VALUES (4, 2, '2', '第二人回复第2条留言2次', NULL);
INSERT INTO `reply` VALUES (5, 2, '1', '第一人回复第2条留言', NULL);

SET FOREIGN_KEY_CHECKS = 1;

四、查询SQL


SELECT
	B.RID id,
	A.OID cid,
	B.RUID uid,
	B.RRECONTENT recontent,
	B.RRETIME retime,
	A.OCONTENT content,
	B.RUNAME NAME,
	A.OUNAME author 
FROM
	(
SELECT
	O.uid OUID,
	O.content OCONTENT,
	U.`name` OUNAME,
	O.id OID 
FROM
	`user` AS U,
	`online` AS O 
WHERE
	U.uuid = O.uid 
	) A,
	(
SELECT
	U.`name` RUNAME,
	R.recontent RRECONTENT,
	R.retime RRETIME,
	R.cid RCID,
	R.uid RUID,
	R.id RID 
FROM
	`user` AS U,
	reply AS R 
WHERE
	U.uuid = R.uid 
	) B 
WHERE
	A.OID = B.RCID



五、查询结果

用户、留言、评论表的查询以及展示_第1张图片


六、实体类

用户、留言、评论表的查询以及展示_第2张图片

你可能感兴趣的:(MySQL)