我试图加入 2 个表并计算结果。我有这些桌子;
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `segments`
-- ----------------------------
DROP TABLE IF EXISTS `segments`;
CREATE TABLE `segments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`substr_id` int(255) NOT NULL,
`substr` varchar(255) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of `segments`
-- ----------------------------
BEGIN;
INSERT INTO `segments` VALUES ('1', '1', 'book', '2'), ('2', '2', 'ooki', '1'), ('3', '2', 'okin', '1'), ('4', '2', 'king', '1');
COMMIT;
-- ----------------------------
-- Table structure for `words`
-- ----------------------------
DROP TABLE IF EXISTS `words`;
CREATE TABLE `words` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`substr_id` int(11) NOT NULL,
`word` varchar(255) NOT NULL,
`cleaned` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of `words`
-- ----------------------------
BEGIN;
INSERT INTO `words` VALUES ('1', '1', 'book', '0'), ('2', '2', 'booking', '0'), ('3', '2', 'booking', '0'), ('4', '2', 'booking', '0');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
用 sql + 数据改变了问题。
我试图 :
SELECT words with there count from segments.count based upon there substr_id.
任何人都可以帮我完成这件事吗?我不太擅长 SQL,有红色文档和一些教程,但不太明白我应该如何在单个查询中完成上述所有操作。