0

I have to select data from two tables with following criteria,

lets say there are two tables as,,

Table one

id | itemName | Quantity | companyName

1    bread      25         the Baker pvt ltd
2    butter     30         green famers

Table two

id | itemName | itemPrice

1    bread      30.50      
6    jam        80.25

what I need is,

select items out of two tables which their ids are matching and the quantities of them should be multiplied by the unit price if ids are matching. The rows which don't have matching ids should be selected but their quantities should not multiplied.

1
  • Where is the unit price? Commented Sep 7, 2011 at 10:32

2 Answers 2

1
   SELECT o.id, o.itemName, o.companyName, o.Quantity * IFNULL(t.itemPrice, 1) total
     FROM one o
LEFT JOIN two t 
       ON o.id = t.id
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should work ...

Select a.id, a.itemName, a.companyName, a.Quantity * IFNULL(b.itemPrice,1) As total
From table1 as a 
Left Join table2 as b on a.id = b.id

2 Comments

thanx a lot for your answer, with this query i can get all the rows that the id is match, when there is no matching ids of each tables how can I select them also?
I think I get what you mean. I think you want a FULL OUTER JOIN instead then. This will return data from both tables even if there is no match. You'll want to add a few more columns in if this is the case because if the LHS table doesn't have a match, you will have NULL for all values currently being selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.