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"
Related
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 1 year ago.
Improve this question
I have 2 tables Product and Vendors. In my Product table, I have a primary key and one foreign key VendorID which references the primary key of the Vendors table:
So, what I was trying to do is to determine which products have a quantity of less than 1000 then display the product's vendor ID and name.
I tried doing this:
SELECT VendorID, Name
FROM Vendors, Product
WHERE Quantity < 1000;
but I get an error
Ambiguous column name 'VendorID'
This is the only solution I can think of since I am just only a beginner in using SQL
I think the output must go like this
VendorID Name
--------------------------------------------
V00002 Liwayway Marketing Corporation 8
V00003 Monde Nissin
Try this:
USE myDB;
SELECT v.VendorID, v.Name
FROM Vendors v JOIN Product p
ON v.VendorID = p.VendorID
WHERE p.Quantity < 1000;
It is happening because your program doesn't know which Vendors or Product table to choose from.
I would try to make a union, like NATURAL JOIN which will make your tables "combine", else, you will get every row of Vendors paired with every row of Product.
Plus: the tables are different, so you have to "pair" them so you can use attributes from the two of them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a table SQL server with two joining or bridging tables because of the many-to-many relationship.
I wonder if anyone can write the query to retrieve data or perform CRUD operations for this table that updates all these three tables.
Please check the attached Diagram particularly the table (Case, Violence_type, and Referral table in the middle where it has a bridging table).
Click to see DB diagram
Yours, omer
So you're trying to link e.g. Case to Referral via the association table Case_Referral? So what's the issue you're facing?
This is a pretty simple, straightforward SQL statement - SELECT from Case, join on Case_Referral via the case_id key, then join to Referral using the referral_id, and specify which columns from each table you need:
SELECT
c.user_name, c.date as CaseDate, c.priority, c.case_status,
r.date AS ReferralDate, r.referral_name
FROM
dbo.Case c
INNER JOIN
dbo.Case_Referral cr ON c.case_ID = cr.case_ID
INNER JOIN
dbo.Referral r ON cr.referral_ID = r.referral_ID
So what is the issue / problem you're not understanding?
You can use the same "technique" to join the other m:n relationships.
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';
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get the total cost of all the invoices for a customer. Ideally the end format will be two columns [customer name] and [total of invoices]. I have broken it down into the parts so far so I can check and better understand the process of joining the tables and have done a calculation to get the total of items on each invoice but now I am stuck.
As you can see from my screenshot ( Had to link to my google docs as I couldn't post the image up here - sorry) I am getting the company name listed multiple times. Once for each item and also for each invoice number and then item. How can I change my query to show the customer name only once with the corresponding totals of all the invoices combined?
I have lines 3 and 4 as comments of what I think is next so I can work this in steps before fine tuning the query to my desired output.
Thanks
Select Customer.CustName, Sum(InvoiceItem.Quantity*Item.ItemPrice) As TotalValue
From Customer
Inner Join Invoice On Customer.CustABN = Invoice.CustABN
Inner Join InvoiceItem On Invoice.InvoiceNo = InvoiceItem.InvoiceNo
Inner Join Item On InvoiceItem.ItemNo = Item.ItemNo
Group By Customer.CustName
Something like this should work using SUM and GROUP BY:
SELECT CustomerName, SUM(itemPrice * qty) InvoiceTotal
FROM YourTables With Your Joins
GROUP BY CustomerName
If you posted your entire query above, I could copy and paste into the example. But this should get you going in the right direction.
grouping could help, also you need to check if your dbms allows grouping without using agregate functions (some DBMS do not allw it, and return misleading results).
multiple companies is because of the relation company-invoice-product i guess.