Reputation: 25
I am running a query and I am getting duplicated rows. I dont understand why this is. Here is my Query:
SELECT c.FName,
p.ProductName,
s.Description,
s.Quantity,
s.Price
FROM customers c,
products p,
sellers s,
requests r
WHERE c.ID = s.CID
AND p.ProductID = s.ProductID
AND r.ProductID = s.ProductID
AND r.C_ID = 3
AND r.MatchType = 'Price'
ORDER BY s.Price ASC
=======edit=======
Okay here is an edit of what the values in the Requests table looks like. NOTE: CID 2=DAZ(generally a seller), 3=Paul (Generally purchase guy) and 5= compny1 (generally a seller) NOTE: product id 1= apple, product id 2=pear, productid 3= berries, product id4=orange
The Request table looks like this after selecting records MatchType=Price and cust ID=3:
requestid | cid | productid | Quantity | Price | matchtype
------------------------------------------------------------------
1 3 1 3.0 2.00 price
3 3 4 4.0 2.50 price
4 3 3 2.5 2.00 exact
5 3 2 3.0 3.50 exact
6 3 3 3.0 2.00 exact
7 3 1 10.0 7.00 price
and here is the sellers table
promotionID | cid | productid | Quantity | Price | description
------------------------------------------------------------------
1 2 4 5.0 2.99 oranges
2 2 3 1.5 1.00 hand strawberries
3 2 3 2.5 2.00 l stawberries
4 2 2 3.0 3.00 pear
5 5 1 5.0 5.00 royal apples fm appleco.
6 2 1 6.0 5.50 sweet apples
AFTER RUNNING THE QUERY I HAVE TRIED BOTH THE SUGGESTED JOINS AND THE ONE IN THIS QUESTION I KEEP GETTING THIS AS OUTPUT
FName ProductName Description Quantity Price
daz Oranges Fresh and sweet oranges. 5.0 2.99
compny1 Apple royal apples fm appleco. 5.0 5.00
compny1 Apple royal apples fm appleco. 5.0 5.00
daz Apple sweet apples 6.0 5.50
daz Apple sweet apples 6.0 5.50
I don't get why I am recieving rows that are repeating. The requested product id must be = sellers product id to match the requested products to the available products and the customerId selected in this case is 3...
I dont get why the last 4 records repeat them self? Why may this be??
Technically speaking only 4 records should be shown. i.e. records on rows.. 1,2 and 3
SUGGESTION/OBSERVATION OKay, After having looked at this... Do you think the rows are repeated because the productID1=apple has been requested by the same customer twice with different quantities???
requestid | cid | productid | Quantity | Price | matchtype
------------------------------------------------------------------
1 3 1 3.0 2.00 price
7 3 1 10.0 7.00 price
Upvotes: 1
Views: 721
Reputation: 61
Your need to use inner join for "filtering" the rows. try this:
select c.FName, p.ProductName, s.Description, s.Quantity, s.Price
FROM requests r
inner join sellers s on r.ProductID = s.ProductID
inner join products p on p.ProductID=s.ProductID
inner join customers c on c.ID=s.CID
where r.C_ID = 3 AND r.MatchType='Price'
ORDER BY s.Price ASC
hope that i don't have any mistake here (its late here), but its the main idea. for columns that exist in two tables and you wish to use the for filtering use inner join, for filtering from one table use the were clause .. (that the theory on one leg) ...
--- edit ----
this query can show the diffidence between the requests ...
select c.FName, p.ProductName, s.Description, s.Quantity, s.Price, r.demandid as 'Request ID'
FROM requests r
inner join sellers s on r.ProductID = s.ProductID
inner join products p on p.ProductID=s.ProductID
inner join customers c on c.ID=s.CID
where r.C_ID = 3 AND r.MatchType='Price'
ORDER BY r.demandid s.Price ASC
Upvotes: 1
Reputation: 327
select c.FName, p.Name, s.Description, s.Quantity, s.Price
FROM customers c
left join sellers s on c.ID = s.cid
left join requests r on r.ProductID = s.ProductID
left join products p on p.productid = s.productid
where r.C_ID = 1
AND r.MatchType='Price'
ORDER BY s.Price ASC
I setup a fiddle for it SQL Fiddle and threw some dummy data in. The code works if I set the data up correctly.
Upvotes: 0