应用程序的视图包含一个page,其中显示了一堆轮播(每个轮播= 一个标签),每个轮播应包含5个products:
+-------------------------------------+
|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
+--------------+------------+------+-----+---------+----------------+
| 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子查询'
需要帮助。
您也许可以使用标量子查询来做您想做的事情:
p.id <= ANY (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
)
ANY
用于处理少于5个标签的情况。
注意:这假设您要按顺序订购产品ID。