In the table t_contents
stored an a database MySql version 8.0.17
I have these rows
+-------------------------+-------+-------+-----+
| Contents | sID_c | sID_p | sID |
+-------------------------+-------+-------+-----+
| Content 1 | 1 | NULL | 1 |
| - Gaio Giulio Cesare | 1 | NULL | 2 |
| Veni, vidi, vici. | 1 | NULL | 3 |
| Carpe diem. | 1 | NULL | 4 |
| Ubi maior minor cessat. | 1 | NULL | 5 |
| - Quinto Orazio Flacco | 1 | NULL | 6 |
| Condicio sine qua non. | 1 | NULL | 7 |
| Carthago delenda est. | 1 | NULL | 8 |
| Content 2 | 2 | NULL | 9 |
| - Marco Porcio Catone | 2 | NULL | 10 |
| Inter sidera versor. | 2 | NULL | 11 |
| Content 3 | 3 | NULL | 12 |
| - Marco Tullio Cicero | 3 | NULL | 13 |
| Vivere est cogitare | 3 | NULL | 14 |
+-------------------------+-------+-------+-----+
In the column Contents of the table t_contents:
Content 1
it's a Section- Gaio Giulio Cesare
it's a ChapterVeni, vidi, vici
, Carpe diem.
and Ubi maior minor cessat
. are the paragraph of ChapterI don’t know the number of Section, Chapter and the paragraph of Chapter … are variables…
I need count these rows and group these rows for sID_c
field, to this return
mysql> SELECT
Contents,
COUNT(*) q
FROM
`t_contents`
GROUP BY
sID_c;
+-----------+----+
| Contents | q |
+-----------+----+
| Content 1 | 8 |
| Content 2 | 3 |
| Content 3 | 3 |
+-----------+----+
3 rows in set (0.02 sec)
And using the q
value, min and max, for return this scheme
+-------------------------+-------+-------+-----+
| Contents | sID_c | sID_p | sID |
+-------------------------+-------+-------+-----+
| Content 1 | 1 | 1.1 | 1 |
| - Gaio Giulio Cesare | 1 | 1.2 | 2 |
| Veni, vidi, vici. | 1 | 1.3 | 3 |
| Carpe diem. | 1 | 1.4 | 4 |
| Ubi maior minor cessat. | 1 | 1.5 | 5 |
| - Quinto Orazio Flacco | 1 | 1.6 | 6 |
| Condicio sine qua non. | 1 | 1.7 | 7 |
| Carthago delenda est. | 1 | 1.8 | 8 |
| Content 2 | 2 | 2.1 | 9 |
| - Marco Porcio Catone | 2 | 2.2 | 10 |
| Inter sidera versor. | 2 | 2.3 | 11 |
| Content 3 | 3 | 3.1 | 12 |
| - Marco Tullio Cicero | 3 | 3.2 | 13 |
| Vivere est cogitare | 3 | 3.3 | 14 |
+-------------------------+-------+-------+-----+
Any help would greatly appreciate... Thank you.
-- ----------------------------
-- Table structure for t_contents
-- ----------------------------
DROP TABLE IF EXISTS `t_contents`;
CREATE TABLE `t_contents` (
`Contents` varchar(255) DEFAULT NULL,
`sID_c` int(11) NULL DEFAULT NULL,
`sID_p` varchar(255) DEFAULT NULL,
`sID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`sID`) USING BTREE
) ENGINE = InnoDB;
-- ----------------------------
-- Records of t_contents
-- ----------------------------
INSERT INTO `t_contents` VALUES ('Content 1', 1, NULL, 1);
INSERT INTO `t_contents` VALUES ('- Gaio Giulio Cesare', 1, NULL, 2);
INSERT INTO `t_contents` VALUES ('Veni, vidi, vici.', 1, NULL, 3);
INSERT INTO `t_contents` VALUES ('Carpe diem.', 1, NULL, 4);
INSERT INTO `t_contents` VALUES ('Ubi maior minor cessat.', 1, NULL, 5);
INSERT INTO `t_contents` VALUES ('- Quinto Orazio Flacco', 1, NULL, 6);
INSERT INTO `t_contents` VALUES ('Condicio sine qua non.', 1, NULL, 7);
INSERT INTO `t_contents` VALUES ('Carthago delenda est.', 1, NULL, 8);
INSERT INTO `t_contents` VALUES ('Content 2', 2, NULL, 9);
INSERT INTO `t_contents` VALUES ('- Marco Porcio Catone', 2, NULL, 10);
INSERT INTO `t_contents` VALUES ('Inter sidera versor.', 2, NULL, 11);
INSERT INTO `t_contents` VALUES ('Content 3', 3, NULL, 12);
INSERT INTO `t_contents` VALUES ('- Marco Tullio Cicero', 3, NULL, 13);
INSERT INTO `t_contents` VALUES ('Vivere est cogitare', 3, NULL, 14);
You need to concatenate sID_c
with a number returned by ROW_NUMBER()
window function:
SELECT Contents,
sID_c,
CONCAT(sID_c, '.', ROW_NUMBER() OVER (PARTITION BY sID_c ORDER BY sID)) sID_p,
sID
FROM t_contents
Or if you want to update the table:
WITH cte AS (
SELECT CONCAT(sID_c, '.', ROW_NUMBER() OVER (PARTITION BY sID_c ORDER BY sID)) sID_p,
sID
FROM t_contents
)
UPDATE t_contents t
INNER JOIN cte c ON c.sID = t.sID
SET t.sID_p = c.sID_p
See the demo.