mysql:对添加的连接表使用限制

xlpyo6sf  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(362)

查看

应用程序的视图包含一个页面,该页面显示一组旋转木马(每个旋转木马=一个标签),每个转盘应包含5种产品:

+-------------------------------------+
|Page                                 |
|+-----------------------------------+|
||Tag1                               ||
||+--------+ +--------+    +--------+||
|||Product1| |Product2| .. |Product5|||
||+--------+ +--------+    +--------+||
|+-----------------------------------+|
|...                                  |
|+-----------------------------------+|
||TagN                               ||
||...                                ||
+-------------------------------------+

数据库

页码

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(16) | NO   | UNI | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

页面\u标签

+--------------+------------+------+-----+---------+----------------+
| Field        | Type       | Null | Key | Default | Extra          |
+--------------+------------+------+-----+---------+----------------+
| id           | int(11)    | NO   | PRI | NULL    | auto_increment |
| page_id      | int(11)    | NO   | MUL | NULL    |                |
| tag_id       | int(11)    | NO   | MUL | NULL    |                |
| position     | tinyint(4) | NO   |     | NULL    |                |
+--------------+------------+------+-----+---------+----------------+
CREATE TABLE `pages_tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `page_id` int(11) NOT NULL,
  `tag_id` int(11) NOT NULL,
  `position` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `page_id` (`page_id`),
  KEY `tag_id` (`tag_id`),
  CONSTRAINT `pages_tags_ibfk_1` FOREIGN KEY (`page_id`) REFERENCES `pages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `pages_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=663 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

标签

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(128) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
CREATE TABLE `tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=149 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

产品标签

+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| id         | int(11) | NO   | PRI | NULL    | auto_increment |
| product_id | int(11) | NO   | MUL | NULL    |                |
| tag_id     | int(11) | NO   | MUL | NULL    |                |
+------------+---------+------+-----+---------+----------------+
CREATE TABLE `products_tags` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `tag_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `tag_id` (`tag_id`),
  KEY `bouquet_id` (`product_id`),
  CONSTRAINT `products_tags_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `products_tags_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5698 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

产品

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(128) | NO   | UNI | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=335 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

目标

目标是得到一个输出,其中包含页面上的所有标签,每个标签最多包含5个产品。

查询

以下是我遇到的问题:

SELECT t.name, p.name 
FROM pages a 
LEFT JOIN pages_tags a_t ON a_t.page_id = a.id 
LEFT JOIN tags t ON t.id = a_t.tag_id 
LEFT JOIN products p ON p.id = (
  SELECT p_t.product_id FROM products_tags p_t WHERE p_t.tag_id = t.id LIMIT 1
);`

问题

无法使用 limit 5 :
错误1242(21000):子查询返回超过1行
无法使用 in + limit :
错误1235(42000):此版本的mysql尚不支持“limit&in/all/any/some子查询”
需要帮助。

我要返回的内容(示例)

页码
标签1
产品1
产品2
产品3
产品4
产品5
标签2
产品6
产品7
产品8
产品9
产品10
...
塔根
产品K
productl(但总是最大值为5)

gcmastyq

gcmastyq1#

您可以使用标量子查询执行所需的操作:

p.id <= (SELECT p_t.product_id
         FROM products_tags p_t
         WHERE p_t.tag_id = t.id 
         ORDER BY p_t.product_id
         LIMIT 1 OFFSET 4
        )

但是,如果行数少于5行,则此操作不起作用。在这种情况下,你可以使用 coalesce() :

p.id <= COALESCE( (SELECT p_t.product_id
                   FROM products_tags p_t
                   WHERE p_t.tag_id = t.id 
                   ORDER BY p_t.product_id
                   LIMIT 1 OFFSET 4
                  ), p.id
                )

注意:这假设您希望产品标识按顺序排列。

相关问题