Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm brand new at SQL. I'm trying to display all the bikes that are colored black. I keep getting errors such as not unique tables/alias: Product. What am I missing?
SELECT Vendor.VendorID, Product.ProductID
FROM Product
INNER JOIN Product ON Vendor.VendorID = Product.ProductID
WHERE product.ProductColor = "Black";
SELECT Vendor.VendorID, Product.ProductID
FROM Product
INNER JOIN Product -- <-- this is the problem
ON Vendor.VendorID = Product.ProductID
WHERE product.ProductColor = "Black";
You meant to put Vendor there. Your server is complaining that you joined Product to itself but didn't tell it which of the two uses of "Product" to apply the where condition to.
Try the following. You do not have vendor table but you have mentioned that table column on join. Also, You should always use alias for the tables when you use join as it gives more readability.
SELECT v.VendorID, p.ProductID
FROM Product p
INNER JOIN Vendor v
ON v.VendorID = p.ProductID
WHERE p.ProductColor = 'Black';
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have been trying to figure this out for 4 days now. I am new to this and just can't this to work like it should. Any help would be appreciated.
*Not just looking for the answer
The president of the company wants a list of all orders ever taken. He wants to see the customer name, the last name of the employee who took the order, the shipper who shipped the order, the product that was ordered, the quantity that was ordered, and the date on which the order was placed. [Hint: You will need to join the following tables: Customers, Employees, Shippers, Orders, OrderDetails, Products, and to get all of the necessary information.]
Think about what you are trying to do here. You'll need to join together multiple tables to build the desired dataset. The type of JOINs you should be using will likely associate foreign keys from one table with the primary key of another. Please review JOINs if this is not clear.
Here is an example query that may get you on the right track. The column names used will likely vary, and I'll let you figure out how to get Quantity in the dataset (the quantity is probably in OrderDetails or perhaps you need to aggregate by product ID):
SELECT Customers.name, Employees.lastName, Shippers.name, Products.name,OrderDetails.orderDate
FROM Orders
JOIN OrderDetails ON OrderDetails.id = Orders.orderDetailId
JOIN Customers ON Customers.customerId = Orders.customerId
JOIN Employees ON Employees.employeeId = Orders.employeeId
JOIN Shippers ON Shippers.Id = OrderDetails.shipperId
JOIN Products ON OrderDetails.ProductId = Products.Id;
I should note that this is formatted for T-SQL and depending on your DBMS syntax may vary.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My table looks like this -
Explanation
I'm working on an online shop that sells tours and spa. I'm showing all together on one page.
What I need to select?
1. All spa products (based on "Spa" column), no conditions.
2. All parent tours that have children with an upcoming date.
Meaning
Products ID 1, 4, 5.
Why?
Product 6 have a child, but from 1999. And although product 1 have one child at 2000, it has another one in 2017. All spa products are selected by default, without conditions.
I hope I made my question clear as I could. I would appreciate any help, my SQL is really bad.
Your data structure isn't great. Really, as you have a parent -> child relationship as well as special types, it would be better to have three tables e.g. Product, ProductType and ProductItem or some such nomenclature. The parent table contains the ID, name and typeId (having a flag limits you to only two types, you may chooes to have more). Your child table contains a ID, parent ID, name and date. Then you can simply use a foreign key constraint to link the two and do some simple SQL to join it all up e.g.
SELECT pt.Name, pi.Name, pi.Date from ProductItem pi
INNER JOIN Product p on p.id = pi.parentID
INNER JOIN ProductType pt on p.typeId = pt.id
WHERE pi.date > now() --The "Now" part depends on your RDBMS as it's different on different systems
AND pt.name = "TOUR"
UNION
SELECT pt.Name, p.Name, 'N/A' from Product p
INNER JOIN ProductType pt on pt.id = p.typeId
WHERE pt.name = "SPA"
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two tables, and I want to connect the tables by CustomerID (CustomerID is same for two tables). So I used my query like:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay ON Cus.CustomerID = Pay.CustomerID
WHERE
CustomerID = 2
And it shows an error:
'CustomerID' in where clause is ambiguous
How can I resolve ambiguous column name error in a way that does not add the table name before CustomerID?
Since it doesn't know which CustomerID to use in your where clause CustomerID = 2, you should specify which to use:
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
WHERE
Cus.CustomerID = 2
It doesn't matter if you use Cus.CustomerID or Pay.CustomerID since they will always be the same in your current statement (since you equal them in your join).
The ambigious part is here:
WHERE
CustomerID = 2
Both tables have this column you need to be explict either:
Cus.CustomerID
or
Pay.CustomerID
[I]n a way that does not add the table name before CustomerID
You can use a sub-query.
SELECT CustomerID
FROM
(
SELECT
Cus.CustomerID
FROM
Customers AS Cus
JOIN
Payments AS Pay
ON
Cus.CustomerID = Pay.CustomerID
) query
WHERE CustomerID = 2
Is this a good way? No. This may force the query plan to use a scan instead of a seek if CustomerID is an indexed field (which it should be since this is being joined on).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm having a hard time joining three separate tables in Oracle. I'm never joined three tables before so I'm not well versed. My theory is below:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON order_num, stock_num
INNER JOIN stock
ON stock_num, description
Essentially, I'm trying to start with the ORDERS table and pull the customer number (customer_num) specifically customer number 104 -108 and the order_num from the orders table. Then attach the orders table to the Items table and attach the order_num and stock_num, and lastly attach the stock table and pull out the stock_num and description.
Your syntax is all over the place, and wouldn't work for a two-table join, or even querying a single table. Not sure where you got WHEN from, or the order of your clauses. Please review the SQL reference to see how to join and how to filter. Anyway...
You seem to want something like this:
SELECT o.customer_num, o.order_num, i.stock_num, s.description
FROM orders o
INNER JOIN items i ON i.order_num = o.order_num
INNER JOIN stock s ON s.stock_num = i.stock_num
WHERE o.customer_num BETWEEN 104 AND 198;
The WHERE clause is being applied to the orders table to restrict which customers' orders you get. I've assumed from your description that the orders and items tables have a common order_num column you can join on; and that the items and stock tables have a common stock_num column you can join on.
As OldProgrammer said, it would be helpful to include your table schemas in the question so assumptions don't need to be made, and showing sample data and expected output for that data would clarify what you're trying to do.
It should look something like this (based on the limited information about the schema)...
SELECT
O.CUSTOMER_NUM,
O.ORDER_NUM,
S.STOCK_NUM,
S.DESCRIPTION
FROM
ORDERS O,
ITEMS I,
STOCK S
WHERE
I.CUSTOMER_NUM >= 104
AND I.CUSTOMER_NUM <= 198
AND O.ORDER_NUM = I.ORDER_NUM
AND I.STOCK_NUM = S.STOCK_NUM
The syntax for a JOIN is:
TableExpression [ INNER ] JOIN TableExpression
{
ON booleanExpression | USING clause
}
The booleanExpression is what the tables will be linked by. So, in your example, something like this:
SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
ON orders.<some_column> = items.<some_column>
INNER JOIN stock
ON items.<some_column> = stock.<some_column>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have multiple tables that I want to pull data from to make an item list
Below are the columns I want to capture in my list
item.code, item.description, itemtype.description, subcategory.description, UOM.description
The tables and columns are listed below
Items table - This is the main source of information
The columns I want to display are
item.code, item.description,
Itemtype - Items is linked to item type by Item_type_ID
The column I want to display is
itemtype.description
Subcategories - Items is linked to the subcategories by subcateg_id
The column I want to display is
subcategory.description
Units of measure Items is link to the UOM by uom.id
The column I want to display is
uom.description
Any help would be greatly appreciated. I have been playing with JOINS with varying results.
SELECT i.code,
i.description,
it.description,
sc.description,
um.description
FROM items AS i
INNER JOIN itemtype AS it
ON i.item_type_id = it.id
INNER JOIN subcategory AS sc
ON i.subcateg_id = sc.id
INNER JOIN uom AS um
ON i.uom_id = um.id