I'm making a mySql SELECT:
SELECT name, description, brand, Providers.name, Categories.name, sellingPrice, quantity
FROM Products
INNER JOIN Providers ON Products.idProvider = Providers.id
INNER JOIN Categories ON Products.idCategory = Categories.id
WHERE category = 'tools';
but I become this error:
Error Code: 1052. Column 'name' in field list is ambiguous
even if I specified 'Table.column' (Providers.name, Categories.name).
Some help please ??
Did you try removing the name field in the beginning? Try this code
SELECT description, brand, Providers.name, Categories.name, sellingPrice, quantity
FROM Products
INNER JOIN Providers ON Products.idProvider = Providers.id
INNER JOIN Categories ON Products.idCategory = Categories.id
WHERE category = 'tools';
Similarly provide proper Table.Column mapping in the select.
If you have a name field in Products table, include Products.name in select
Whenever you join tables , you may find same column names in multiple tables. The SQL Engine doesnt know which to pick.So in order to differentiate you need to specify proper mappings. You can also specify simple Alias names instead of full Table names.
Simple rule: Qualify all column names when you write a query. Always.
If you give tables reasonable aliases, this is easy. So your query should look something like this:
SELECT p.name, p.description, p.brand, pr.name, c.name, p.sellingPrice, p.quantity
FROM Products p JOIN
Providers pr
ON p.idProvider = pr.id JOIN
Categories c
ON p.idCategory = c.id
WHERE c.category = 'tools';
I am guessing what tables the columns are coming from, so the qualified names may not be correct (your question doesn't provide this information).
Related
I have 4 tables
Products, Products_Location, Stores, Locations
Products = [Product_ID, P_Name, Price]
Products_Location = [Location_ID,Product_ID]
Locations = [Location_ID,Stores_ID, L_Name]
Stores = [Stores_ID, S_Name]
I'm trying to show columns from each table in 1 table but its not working
I tried to use inner join twice but didnt work (it work if I use 1 inner join)
here's my code
"SELECT Products.P_Name, Products.Price, Stores.S_Name, Locations.L_Name, Products.Product_ID
From Products, Locations
INNER JOIN Stores ON Locations.Stores_ID = Stores.Stores_ID
INNER JOIN Products_Location ON Products.Product_ID = Products_Location.Product_ID
Where P_Name LIKE '%$search%' OR Price LIKE '%$search%' OR S_Name LIKE '%$search%' OR L_Name LIKE '%$search%'
ORDER BY Price ASC";
As it appears you are relatively new to SQL, instead of getting pounded on your new posts, let me help a bit. Writing your queries with JOINS (left, right, full, outer, etc) are all typically based on TableA some join to TableB. So, try to think of your tables in a mental image. What is the relationship between them. THAT is the join. If one table is joined to multiple, then treat that as its own JOIN. I typically try to indent showing the level where TableA joins to TableB (or TableC, D, E, etc).
Then, also you can get used to using "alias" names so you dont have to keep writing LONG table name references. Taking your original query, it would be something more like...
select
p.p_name,
p.price,
s.s_name,
l.l_name,
p.product_id
From
Products p
-- first get the JOIN from product to its locations where available
JOIN Products_Location pl
on p.product_id = pl.product_id
-- now, from the product location to the location table.
-- notice my indentation to show its under the product_location
-- and not directly from the product table. Visually seeing the
-- hierarchical chain can help writing queries.
JOIN Locations l
on pl.location_id = l.location_id
-- and now, indent once more from location to the stores
JOIN Stores s
on l.stores_id = s.stores_id
where
-- NOW, you can put in your filtering criteria. But if ever a left or right
-- based join, you would just add that part of the criteria directly within
-- the JOIN portion
p.p_name like '%$search%'
OR p.Price LIKE '%$search%'
OR s.S_Name LIKE '%$search%'
OR l.L_Name LIKE '%$search%'
ORDER BY
p.Price ASC
Also, always try to qualify all columns in your query with table.column or alias.column so others know which table(s) the columns come from to prevent any ambiguity in the call.
You are performing a cross join on Products and Locations. Is it not your aim to instead join Products and Locations via the Products_Location table on Products.Product_ID = Products_Location.Product_ID and Products_Location.Location_ID = Locations.Location_ID?
SQL newbie here.
So we have 3 tables:
categories(cat_id,name);
products(prod_id,name);
relationships(prod_id,cat_id);
It is a one-to-many relationship.
So, given a category name say "Books". How do I find all the products that come under books?
As an example,
categories(1,Books);
categories(2,Phones);
products(302,Sherlock Holmes);
relationships(302,1);
You need to JOIN the three tables.
SELECT p.*
FROM relationships r
INNER JOIN products p
ON p.prod_id = r.prod_id
INNER JOIN categories c
ON c.cat_d = r.cat_id
WHERE c.name = 'Books'
You have to join tables on related columns and specify WHERE clause to select all records where category name = 'Books'
SELECT p.*
FROM categories c
JOIN relationships r ON c.cat_id = r.cat_id
JOIN products p ON r.prod_id = p.prod_id
WHERE c.name = 'Books' -- or specify parameter like #Books
In SQL you often join related tables and beginners tend to join, whatever the situation. I would not recommend this. In your case you want to select products. If you only want to show products data, select from products only. You want to select products that are in the category 'Books' (or for which exists an entry in category 'Books'). Hence use an IN or EXISTS clause in order to find them:
select * from products
where prod_id in
(
select prod_id
from relationships
where cat_id = (select cat_id from categories where name = 'Books')
);
Thus you get a well structured query that tells the reader easily how the tables are related and what data you are actually interested in. Later, with different tables and data to select, this may keep you from duplicate result rows that you must get rid of by using DISTINCT or from getting wrong aggregates (sums, counts, etc.), because of mistakenly considering records multifold.
try this:
select p.Prod_id,p.name
from products p inner join relationships r on
p.prod_id = r.prod_id
where r.cat_id = (select cat_id from categories where name = 'books')
or
select p.Prod_id,p.name
from products p inner join relationships r on
p.prod_id = r.prod_id inner join categories c on c.cat_id = r.cat_id
where c.name = 'books'
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
I have three tables is question. categories, vocabulary & tex. I am trying to figure out how to have multiple joins in my query, i thought you can just add as many joins as you wanted, as long as you reference them properly.
So, the following two work perfectly on there own:
1.
SELECT
categories.ID AS ID,
categories.ParentID AS ID,
vocabulary.value AS Name
FROM categories
INNER JOIN vocabulary
ON categories.sid=vocabulary.sid
WHERE vocabulary.langid=1
2.
SELECT
categories.ID AS ID,
categories.ParentID AS ID,
tex.value AS Description
FROM categories
INNER JOIN tex
ON categories.tid=tex.tid
WHERE tex.langid=1
However, if i try to combine them as follows, it does not work.
categories.ID AS ID,
categories.ParentID AS ID,
vocabulary.value AS Name
tex.value AS Description
FROM categories
INNER JOIN tex
ON categories.tid=tex.tid
WHERE tex.langid=1
INNER JOIN vocabulary
ON categories.sid=vocabulary.sid
WHERE vocabulary.langid=1
Any ideas?
Thanks in advance
John
In MySQL, when you have columns with the same name, one of them will only be shown. You need to identify them uniquely by supplying ALIAS. And you can either put the condition on the ON clause or WHERE clause which could yield the same result since it uses INNER JOIN.
SELECT categories.ID AS CategoryID,
categories.ParentID AS CategoryParentID,
vocabulary.value AS Name
tex.value AS Description
FROM categories
INNER JOIN tex
ON categories.tid = tex.tid
INNER JOIN vocabulary
ON categories.sid = vocabulary.sid
WHERE vocabulary.langid = 1 AND
tex.langid = 1
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.