Mysql 如何最好地组合和优化这两个查询?

Mysql 如何最好地组合和优化这两个查询?,mysql,refactoring,database-design,Mysql,Refactoring,Database Design,这是我的主查询,它将线程信息作为一行拉入,它缺少投票数,目前我正在用第二个查询拉入 SELECT Group_concat(t.tag_name) AS `tags`, `p`.`thread_id`, `p`.`thread_name`, `p`.`thread_description`, `p`.`thread_owner_id`, `p`.`thread_view_count`,

这是我的主查询,它将线程信息作为一行拉入,它缺少投票数,目前我正在用第二个查询拉入

SELECT   Group_concat(t.tag_name) AS `tags`,
         `p`.`thread_id`,
         `p`.`thread_name`,
         `p`.`thread_description`,
         `p`.`thread_owner_id`,
         `p`.`thread_view_count`,
         `p`.`thread_reply_count`,
         `p`.`thread_comment_count`,
         `p`.`thread_favorite_count`,
         `p`.`thread_creation_date`,
         `p`.`thread_type_id`,
         `p`.`thread_edited_date`,
         `u`.*,
         `x`.*,
         `t`.*

FROM     `shoop_posts` AS `p`
         INNER JOIN `shoop_users` AS `u`
           ON u.user_id = p.thread_owner_id
         LEFT JOIN `shoop_tags_map` AS `x`
           ON x.thread_id = p.thread_id
         LEFT JOIN `shoop_tags` AS `t`
           ON t.tag_id = x.tag_id


WHERE    (p.thread_id = '1')
GROUP BY `p`.`thread_id` 
我的第二个问题是每个线程的投票数:

SELECT Sum(vote_value)
FROM   shoop_votes
       INNER JOIN shoop_vote_codes
         ON shoop_votes.vote_type = shoop_vote_codes.vote_type
WHERE  thread_id = 1
       AND shoop_votes.vote_type = 3
        OR shoop_votes.vote_type = 2 
投票类型为2表示赞成票,3表示反对票。这是您需要的模式,以及一些示例数据:

CREATE TABLE `shoop_posts` (

  `thread_id` int(11) unsigned NOT NULL auto_increment,

  `thread_name` text,

  `thread_description` text,

  `thread_parent_id` int(11) default NULL,

  `thread_owner_id` int(11) default NULL,

  `thread_view_count` int(11) default NULL,

  `thread_reply_count` int(11) default NULL,

  `thread_comment_count` int(11) default NULL,

  `thread_favorite_count` int(11) default NULL,

  `thread_creation_date` timestamp NULL default NULL,

  `thread_type_id` int(11) default NULL,

  `thread_edited_date` timestamp NULL default NULL,

  PRIMARY KEY  (`thread_id`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;



-- ----------------------------

-- Records of shoop_posts

-- ----------------------------

INSERT INTO `shoop_posts` VALUES ('1', 'Shoop that', '\r\n<img class=\"image-shoop\" src=\"\">\r\n\r\n<p>test:<br>\r\n\r\n\r\n</p>', null, '2', '217', '0', '0', '0', '2010-01-10 02:06:25', '1', null);



-- ----------------------------

-- Table structure for `shoop_tags`

-- ----------------------------


CREATE TABLE `shoop_tags` (

  `tag_id` int(11) NOT NULL auto_increment,

  `tag_name` varchar(11) default NULL,

  PRIMARY KEY  (`tag_id`)

) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;



-- ----------------------------

-- Records of shoop_tags

-- ----------------------------

INSERT INTO `shoop_tags` VALUES ('1', 'mma');

INSERT INTO `shoop_tags` VALUES ('2', 'strikeforce');

INSERT INTO `shoop_tags` VALUES ('3', 'ufc');



-- ----------------------------

-- Table structure for `shoop_tags_map`

-- ----------------------------

DROP TABLE IF EXISTS `shoop_tags_map`;

CREATE TABLE `shoop_tags_map` (

  `map_id` int(11) NOT NULL auto_increment,

  `tag_id` int(11) default NULL,

  `thread_id` int(11) default NULL,

  PRIMARY KEY  (`map_id`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;



-- ----------------------------

-- Records of shoop_tags_map

-- ----------------------------

INSERT INTO `shoop_tags_map` VALUES ('1', '1', '1');

INSERT INTO `shoop_tags_map` VALUES ('2', '2', '2');

INSERT INTO `shoop_tags_map` VALUES ('3', '1', '2');

INSERT INTO `shoop_tags_map` VALUES ('4', '3', '1');

INSERT INTO `shoop_tags_map` VALUES ('5', '3', '2');




-- ----------------------------

-- Table structure for `shoop_vote_codes`

-- ----------------------------


CREATE TABLE `shoop_vote_codes` (

  `vote_type` smallint(1) NOT NULL default '0',

  `vote_value` smallint(2) default NULL,

  PRIMARY KEY  (`vote_type`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ----------------------------

-- Records of shoop_vote_codes

-- ----------------------------

INSERT INTO `shoop_vote_codes` VALUES ('2', '1');

INSERT INTO `shoop_vote_codes` VALUES ('3', '-1');



-- ----------------------------

-- Table structure for `shoop_votes`

-- ----------------------------

DROP TABLE IF EXISTS `shoop_votes`;

CREATE TABLE `shoop_votes` (

  `thread_id` int(11) NOT NULL default '0',

  `user_id` int(11) NOT NULL default '0',

  `vote_type` smallint(1) NOT NULL default '0',

  PRIMARY KEY  (`thread_id`,`user_id`,`vote_type`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;



-- ----------------------------

-- Records of shoop_votes

-- ----------------------------

INSERT INTO `shoop_votes` VALUES ('1', '1', '2');

INSERT INTO `shoop_votes` VALUES ('1', '2', '2');

INSERT INTO `shoop_votes` VALUES ('1', '3', '3');
创建表“shoop_posts”(
`线程id`int(11)无符号非空自动增量,
`线程名称`文本,
`线程描述`文本,
`线程\u父线程\u id`int(11)默认为空,
`线程\u所有者\u id`int(11)默认为空,
`线程\视图\计数`int(11)默认为空,
`线程\应答\计数`int(11)默认为空,
`线程注释计数`int(11)默认为空,
`线程\u收藏夹\u计数`int(11)默认为空,
`线程_创建_日期`时间戳为空默认为空,
`线程类型_id`int(11)默认为空,
`线程_编辑_日期`时间戳为空默认为空,
主键(`thread\u id`)
)ENGINE=MyISAM AUTO_INCREMENT=3默认字符集=utf8;
-- ----------------------------
--shoop_员额记录
-- ----------------------------
插入“shoop\u posts”值('1','shoop that','\r\n\r\n测试:
\r\n\r\n\r\n

',null,'2','217','0','0','0','2010-01-10 02:06:25','1',null); -- ---------------------------- --“shoop_”标记的表结构` -- ---------------------------- 创建表“shoop_标记”( `tag_id`int(11)非空自动增量, `tag_name`varchar(11)默认为空, 主键(`tag_id`) )ENGINE=MyISAM AUTO_INCREMENT=4默认字符集=utf8; -- ---------------------------- --shoop_标签记录 -- ---------------------------- 插入'shoop_tags'值('1','mma'); 插入'shoop_tags'值('2','strikeforce'); 插入'shoop_tags'值('3','ufc'); -- ---------------------------- --“shoop_标记”映射的表结构` -- ---------------------------- 如果存在“shoop_tags_map”,则删除表; 创建表“shoop\u标记\u映射”( `map_id`int(11)非空自动增量, `tag_id`int(11)默认为空, `线程id`int(11)默认为空, 主键(`map\u id`) )ENGINE=MyISAM AUTO_INCREMENT=6默认字符集=utf8; -- ---------------------------- --shoop_标签记录图 -- ---------------------------- 插入'shoop_tags_map'值('1','1','1'); 插入'shoop_tags_map'值('2','2','2'); 插入'shoop_tags_map'值('3','1','2'); 插入'shoop_tags_map'值('4','3','1'); 插入'shoop_tags_map'值('5','3','2'); -- ---------------------------- --“shoop\u投票”代码的表结构` -- ---------------------------- 创建表“shoop\u vote\u code”( `投票类型'smallint(1)不为空默认值'0', `表决值'smallint(2)默认为空, 主键(`vote_type`) )ENGINE=MyISAM默认字符集=utf8; -- ---------------------------- --shoop_投票代码记录 -- ---------------------------- 插入'shoop_vote_code'值('2','1'); 在'shoop_vote_code'中插入值('3','-1'); -- ---------------------------- --“shoop_投票”的表格结构` -- ---------------------------- 如果存在“shoop_投票”,则删除表; 创建表“shoop_投票”( `线程_id`int(11)不为NULL默认值为'0', `用户_id`int(11)不为空默认值'0', `投票类型'smallint(1)不为空默认值'0', 主键(`thread\u id`、`user\u id`、`vote\u type`) )ENGINE=MyISAM默认字符集=utf8; -- ---------------------------- --shoop_投票记录 -- ---------------------------- 插入'shoop_voces'值('1','1','2'); 插入'shoop_voces'值('1','2','2'); 插入'shoop_voces'值('1','3','3');
如果我理解正确,只需使用子查询即可完成所需操作:

SELECT   Group_concat(t.tag_name) AS `tags`,
         `p`.`thread_id`,
         `p`.`thread_name`,
         `p`.`thread_description`,
         `p`.`thread_owner_id`,
         `p`.`thread_view_count`,
         `p`.`thread_reply_count`,
         `p`.`thread_comment_count`,
         `p`.`thread_favorite_count`,
         `p`.`thread_creation_date`,
         `p`.`thread_type_id`,
         `p`.`thread_edited_date`,
         `u`.*,
         `x`.*,
         `t`.*,
         `v`.VoteTotal

FROM     `shoop_posts` AS `p`
         INNER JOIN `shoop_users` AS `u`
           ON u.user_id = p.thread_owner_id
         LEFT JOIN `shoop_tags_map` AS `x`
           ON x.thread_id = p.thread_id
         LEFT JOIN `shoop_tags` AS `t`
           ON t.tag_id = x.tag_id
         LEFT JOIN (SELECT thread_id, Sum(vote_value) as VoteTotal
                    FROM   shoop_votes
                       INNER JOIN shoop_vote_codes
                         ON shoop_votes.vote_type = shoop_vote_codes.vote_type
                    WHERE  shoop_votes.vote_type = 3
                           OR shoop_votes.vote_type = 2
                    GROUP BY thread_id) as `v`
           ON p.thread_id = v.thread_id
WHERE    (p.thread_id = '1')
GROUP BY `p`.`thread_id` 
如果您只保留where last where
p.thread\u id
子句,那么这也将使您获得所有线程