Answer the question
In order to leave comments, you need to log in
Question for SQL experts. How to fix lack of received data (page by page) due to duplication when using JOIN?
Hello! There is a site on one CMS, a shop. The site database contains the following tables:
Products table (cms_con_goods)
Fields: id(product) , other parameters (name, properties, etc.), category_id (product category id)
Category table (cms_con_goods_cats)
Fields: id(categories) , other parameters (name, its properties, etc.), ns_left and ns_right (for compiling a category tree from parent to child)
The output of records (products) itself is carried out by attaching the cms_con_goods_cats_bind table , which has the following structure:
item_id (record id), category_id (category id)
Each time a seller adds a product, let's say for a new product 155 with category 4, a row is created in this table:
item_id, category_id
155, 4
And also, there is an "additional categories" option on the site. It allows you to add products to, in addition to the main, other categories. When a seller adds a product to "additional categories", it simply creates additional rows in the cms_con_goods_cats_bind table .
In the admin panel of the site, the display of records of child categories in the parent category is enabled. Those. if this option were disabled, then the output of records would be simple: entering a category with id = n, we get records with category id n, well, records in which n is indicated as additional (by attaching the cms_con_goods_cats_bind table ).
But since this option is enabled and is very important for us, the output of records is a little more complicated and we are faced with one problem.
The query for getting records is the following:
SELECT i.*, u.nickname as user_nickname, f.title as folder_title
FROM cms_con_goods i
JOIN cms_con_goods_cats_bind as b ON b.item_id = i.id
JOIN cms_con_goods_cats as c ON c.id = b.category_id AND c.ns_left >= '{$category['ns_left']}' AND c.ns_right <= '{$category['ns_right']}'
JOIN cms_users as u ON u.id = i.user_id
LEFT JOIN cms_content_folders as f ON f.id = i.folder_id
WHERE (i.is_parent_hidden IS NULL) AND (i.is_approved = '1') AND (i.is_pub = '1')
ORDER BY i.date_raised desc
LIMIT 0, 15
JOIN cms_con_goods_cats_bind as b ON b.item_id = i.id
JOIN cms_con_goods_cats as c ON c.id = b.category_id AND c.ns_left >= '{$category['ns_left']}' AND c.ns_right <= '{$category['ns_right']}'
Answer the question
In order to leave comments, you need to log in
Do first SELECT DISTINCT i.id of your entire query without ORDER BY and LIMIT.
And then, based on the received id, you form the output, for example (but not necessarily the only way), through WHERE i.id IN (nested SELECT with id selection) already taking into account ORDER BY and LIMIT.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question