"Not unique table/alias" in MySQL - mysql

The code below gives me Not unique table/alias: 'presentations'
I've read several articles about this and I'm pretty sure the solution is to use aliases for my tables. I just can't understand how I create aliases or where I should add them.
Could someone please try to explain it for me?
Thanks!
SELECT categories.name, categories.sub_name, parts.name
FROM categories
INNER JOIN presentations
ON categories.id=presentations.category
INNER JOIN presentations
ON parts.id=presentations.parts
WHERE presentations.id=5;

After table name or field name you can give alias name and you can use it.
SELECT C.name, C.sub_name, PT.name
FROM categories C
INNER JOIN presentations P
ON C.id=P.category
INNER JOIN parts PT
ON PT.id=P.parts
WHERE P.id=5;
In above example C,PT,P is alias name of categories,presentation,parts table respectively.

You are joining the same table 2 times and hence you need to provide the unique alias name. However looks like you are looking for parts table and hence need to join that table
SELECT
categories.name,
categories.sub_name,
parts.name
FROM categories
INNER JOIN presentations ON categories.id=presentations.category
INNER JOIN parts ON parts.id=presentations.parts
WHERE presentations.id=5;
For better readability you can always give some short alias name something as
select
c.name,
c.sub_name,
p.name as parts_name
from categories c
join presentations pr on pr.category = c.id
join parts p on p.id = pr.parts
where pr.id = 5

Related

MYSQL- select and join multiple tables into one JSON

Can you please advise how I can fix this query?
I have 3 DB Tables.
networks.
products.
comments.
The query looked like this before I added the "comments" table (worked as expected):
SELECT networks.*, products.product, products.type
FROM products
JOIN networks ON products.id=networks.product_Id
and now I tried modifing it with the additional table, and it doesn't work:
SELECT
networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
JOIN networks ON products.id=networks.product_Id,
ON comments.id=networks.comment_Id
How can I fix this query?
Thanks for the helpers :)
You joined networks table with condition products.id=networks.product_Id
But for comments table you specified the condition comments.id=networks.comment_Id but you forgot to join the comments table.
try
SELECT networks.*,
products.product,
products.type,
comments.id AS comment_Id,
comments.newLine,
comments.lineComment AS comment,
comments.topComment,
comments.bottomComment
FROM products
JOIN networks ON products.id=networks.product_Id
JOIN comments ON comments.id=networks.comment_Id
I think that this is the syntax you are after:
SELECT
n.*,
p.product,
p.type,
c.id AS comment_id,
c.newLine,
c.lineComment AS comment,
c.topComment,
c.bottomComment
FROM products p
INNER JOIN networks n ON p.id = n.product_id
INNER JOIN comments c ON c.id = n.comment_id
That is: the syntax for multiple joins is FROM ... JOIN ... ON ... JOIN ... ON ....
Note that the use of table aliases shortens the query and makes it easier to read and write.

Join many to many table with others

I have a query that joins 3 tables on product id, here is the code.
SELECT oc_product.product_id, oc_product.image, oc_product.price, oc_product_description.name
FROM oc_product
JOIN oc_product_description
ON oc_product.product_id = oc_product_description.product_id;
I get the product_id, image, price and description, but I need to take the category name too, the product is in a many to many relationships with category table. Looks like this.
[oc_product_to_category * pivot table][1]
And I need to take category name from this table.
[oc_category_description][2]
[1] https://i.stack.imgur.com/5WAKd.png
[2] https://i.stack.imgur.com/YNcLn.png
Thank you so much, I am very stuck with this!
PS: I want to take category name and join with my code with other columns.
You only need to append 2 more JOINs to your query:
Join oc_product → oc_product_to_category to link products with their categories
Then Join oc_product_to_category → oc_category_description to grab the
category details
This way you can include to your query whichever fields you need from any of the joined tables.
SELECT p.product_id, p.image, p.price, d.name, c.name, c.description
FROM oc_product AS p
JOIN oc_product_description AS d
ON p.product_id = d.product_id
JOIN oc_product_to_category AS pc
ON pc.product_id = p.product_id
JOIN oc_category_description AS c
ON c.category_id = pc.category_id;
As you noted, the use of aliases for the actual table names helps to simplify a bit both the SELECT and the JOIN-ON segments of the query.
I've created a quick example here to show the results.

Using two inner join tables

I have come up with two queries, both use an inner join on two different tables.
Query 1
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT, PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY, PRODUCTS.PRICESELL, CATEGORIES.NAME AS CATEGORY
FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID;
Query 2
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT, PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY, PRODUCTS.PRICESELL,STOCKCURRENT.UNITS AS UNIT FROM PRODUCTS INNER JOIN STOCKCURRENT ON STOCKCURRENT.PRODUCT = PRODUCTS.ID;
Both queries run fine on their own, when I try to use both inner joins together I get errors. This is what I came up with on my own. I'm having trouble understanding the syntax to achieve this.
SELECT PRODUCTS.CODE, PRODUCTS.REFERENCE, PRODUCTS.TAXCAT,
PRODUCTS.DISPLAY,PRODUCTS.NAME, PRODUCTS.PRICEBUY,
PRODUCTS.PRICESELL,STOCKCURRENT.UNITS AS UNIT FROM PRODUCTS INNER JOIN
STOCKCURRENT ON STOCKCURRENT.PRODUCT = PRODUCTS.ID, CATEGORIES.NAME AS
CATEGORY FROM PRODUCTS INNER JOIN CATEGORIES ON PRODUCTS.CATEGORY =
CATEGORIES.ID;
Thank you.
Your attempted query has several syntax problems. Assuming you just want to join together the three tables, you may try the following query:
SELECT
p.CODE,
p.REFERENCE,
p.TAXCAT,
p.DISPLAY,
p.NAME,
p.PRICEBUY,
p.PRICESELL,
s.UNITS AS UNIT,
c.NAME AS CATEGORY
FROM PRODUCTS p
INNER JOIN STOCKCURRENT s
ON s.PRODUCT = p.ID
INNER JOIN CATEGORIES c
ON p.CATEGORY = c.ID;
Note that I introduced table aliases here. These aliases can be used elsewhere in the query to avoid having to repeat the entire table name.
By the way, I can also see taking a union of your two original queries. But without expected output, it was not entirely clear what you want.

Select rows with multiple tags... is there a better way?

I am attempting to do a tag system for selecting products from a database. I have read that the best way to achieve this is via a many-to-many relationship as using LIKE '%tag%' is going to get slow when there are a lot of records. I have also read this question where to match on multiple tags you have to do a join for each tag being requested.
I have 3 tables: shop_products, shop_categories and shop_products_categories. And I need to, for example, be able to find products that have both the tag "flowers" and "romance".
SELECT p.sku, p.name, p.path FROM shop_products p
LEFT JOIN shop_products_categories pc1 ON p.sku = pc1.product_sku
LEFT JOIN shop_categories c1 ON pc1.category_id = c1.id
LEFT JOIN shop_products_categories pc2 ON p.sku = pc2.product_sku
LEFT JOIN shop_categories c2 ON pc2.category_id = c2.id
WHERE c1.path = 'flowers' AND c2.path = 'romance'
This is the demo query I'm currently building to check it works before coding the relevant PHP for it and it works. But is this really the best way to do this? I find it hard to believe there isn't a better way to do this than to do a join for each tag searched.
Thanks for any advice. :)
No need to do multiple joins. If you need to match all tags, you can use an IN clause with a subquery like this:
select p.sku, p.name, p.path
from shop_products p
where p.sku in (
select pc.product_sku
from shop_products_categories pc
inner join shop_categories c on pc.category_id = c.id
where c.path in ('flowers', 'romance')
group by pc.product_sku
having count(distinct c.path) = 2
)
Note that you will need to adjust the number 2 to be the number of unique tags you are matching on. Beware in case this is user-entered data and they enter the same tag twice.
SELECT
p.sku, p.name, p.path
FROM
shop_products p
INNER JOIN
(
SELECT A.sku FROM
(
SELECT product_sku sku FROM shop_products_categories
WHERE category_id=(SELECT id FROM shop_categories WHERE path='flowers')
) A
INNER JOIN
(
SELECT product_sku sku FROM shop_products_categories
WHERE category_id=(SELECT id FROM shop_categories WHERE path='romance')
) B
USING (sku)
) flowers_and romance
USING (sku)
;
Make sure you have these indexes:
ALTER TABLE shop_categories ADD INDEX (path,id);
ALTER TABLE shop_categories ADD UNIQUE INDEX (path);
ALTER TABLE shop_products_categories ADD INDEX (product_sku,category_id);

problem with the join query

SELECT *
FROM
productinfo as p ,
category as c
WHERE
c.id IN (p.category) AND
p.pid='T3'
WHERE p.category will return (1,2,3,4,5) from product info table which the id of the category.
Now i need category name used for T3 [product Id] ,but i am getting only the first category name.
Your base query is the following
SELECT * FROM productinfo as p WHERE p.pid = 'T3';
Now you need to pull in categories, per product. This is a many to one relationship, so you need a LEFT JOIN.
SELECT * FROM productinfo as p
LEFT JOIN category as c ON c.id = p.category
WHERE p.id = 'T3'
You need to learn the different types of joins and how they are used. Whenever I see someone use 'FROM table1, table2' 90% of the times it means they don't understand joins and they need a LEFT JOIN instead.
Edit based on your comment
Your datamodel is flawed. Since a product can contain multiple categories, this is really a many-to-many relationship. You should create a product_category table that connects product id's with category id's.