0leg
0leg

Reputation: 14134

MySQL: Use a LIMIT for a JOIN table added

View

The view of the app contains a page which shows a bunch of carousels (each carousel = a tag), each of each should contain 5 products:

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

Database

pages

+-------+-------------+------+-----+---------+----------------+
| 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

pages_tags

+--------------+------------+------+-----+---------+----------------+
| 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

tags

+---------+--------------+------+-----+---------+----------------+
| 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

products_tags

+------------+---------+------+-----+---------+----------------+
| 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

products

+-------------+--------------+------+-----+---------+----------------+
| 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

Goal

The goal is to get an output which will contain all tags on the page, containing the max of 5 products each.

Query

Here is the query I've got before getting stuck:

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
);`

Problems

  1. Unable to use limit 5:

ERROR 1242 (21000): Subquery returns more than 1 row

  1. Unable to use in + limit:

ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Help needed.


What I want to return (example)

Upvotes: 0

Views: 51

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

You may be able to do what you want with a scalar subquery:

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
        )

However, this does not work if there are fewer than 5 rows. In that case, you can use 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
                )

Note: This assumes that you want the product ids in order.

Upvotes: 1

Related Questions