SQL consulta complicada
Tengo esta consulta SQL original que devuelve los resultados de una búsqueda:
SELECT DISTINCT p.products_image, p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) AS final_price FROM products p LEFT JOIN specials s ON p.products_id = s.products_id, products_description pd, categories c, products_to_categories p2c WHERE p.products_status = '1' AND p.products_id = pd.products_id AND pd.language_id = '3' AND p.products_id = p2c.products_id AND p2c.categories_id = c.categories_id AND ((pd.products_name LIKE '%gordo%' OR pd.products_description LIKE '%gordo%')) ORDER BY pd.products_name DESC
Implementación:
He creado una tabla nueva con tags para los artículos:
*Campos:
tags.product_id ___________ tags.tag
1____________________________nuevo
1____________________________gordo
1____________________________especial
2____________________________especial
2____________________________gordo
3____________________________usado
Intento combinar la consulta original con la nueva tabla para obtener en el resultado los nuevos valores de la siguiente manera:
SELECT DISTINCT p.products_image, t.tag, p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, IF s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF (s.status, s.specials_new_products_price, p.products_price) AS final_price FROM products p, tags t LEFT JOIN specials s ON p.products_id = s.products_id, products_description pd, categories c, products_to_categories p2c WHERE p.products_status = '1' AND p.products_id = pd.products_id AND p.products_id = t.product_id AND pd.language_id = '3' AND p.products_id = p2c.products_id AND p2c.categories_id = c.categories_id AND ((pd.products_name LIKE '%gordo%' OR pd.products_description LIKE '%gordo%' OR t.tag LIKE '%gordo%')) ORDER BY pd.products_name DESC
Problema:
Se repiten los registros obtenidos un montón de veces a pesar que SELECT incorpora la cláusula DISTINCT.
EJ:
... Tag__________products_id______products_name...
nuevo___________1_______________Manillar cromado
gordo____________1______________Manillar cromado
especial__________1______________Manillar cromado
gordo____________2______________Horquilla
Desde ya muy agradecido...
SELECT DISTINCT p.products_image, p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) AS final_price FROM products p LEFT JOIN specials s ON p.products_id = s.products_id, products_description pd, categories c, products_to_categories p2c WHERE p.products_status = '1' AND p.products_id = pd.products_id AND pd.language_id = '3' AND p.products_id = p2c.products_id AND p2c.categories_id = c.categories_id AND ((pd.products_name LIKE '%gordo%' OR pd.products_description LIKE '%gordo%')) ORDER BY pd.products_name DESC
Implementación:
He creado una tabla nueva con tags para los artículos:
*Campos:
tags.product_id ___________ tags.tag
1____________________________nuevo
1____________________________gordo
1____________________________especial
2____________________________especial
2____________________________gordo
3____________________________usado
Intento combinar la consulta original con la nueva tabla para obtener en el resultado los nuevos valores de la siguiente manera:
SELECT DISTINCT p.products_image, t.tag, p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, IF s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF (s.status, s.specials_new_products_price, p.products_price) AS final_price FROM products p, tags t LEFT JOIN specials s ON p.products_id = s.products_id, products_description pd, categories c, products_to_categories p2c WHERE p.products_status = '1' AND p.products_id = pd.products_id AND p.products_id = t.product_id AND pd.language_id = '3' AND p.products_id = p2c.products_id AND p2c.categories_id = c.categories_id AND ((pd.products_name LIKE '%gordo%' OR pd.products_description LIKE '%gordo%' OR t.tag LIKE '%gordo%')) ORDER BY pd.products_name DESC
Problema:
Se repiten los registros obtenidos un montón de veces a pesar que SELECT incorpora la cláusula DISTINCT.
EJ:
... Tag__________products_id______products_name...
nuevo___________1_______________Manillar cromado
gordo____________1______________Manillar cromado
especial__________1______________Manillar cromado
gordo____________2______________Horquilla
Desde ya muy agradecido...
1 Respuesta
Respuesta de manicmonday
1