Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having two tables one is sales and one is purchase. In sales I sold one item one time only. I purchased that item two times. When I tried to see that item sales and purchases with a query , that query showing wrong results.

my query is "
select salesDB.sales,purchDB.purchase from salesDB  inner join purchDB  on salesDB.id=purchDB.id where salesDB.id=1


results are like below
SALES PURCHASE
5000 10000
5000 15000

There actually one time only I sold. Value 5000/-. Two times I purchased. Value 25000
so where I did mistake. I created relation to salesDB.id(primary key) and purchaseDB.id

What I have tried:

I googled for solution and found some solutions from this site and other sites also. I tried those but same result.
Posted
Updated 10-Feb-19 21:50pm
v6

1 solution

There is no mistake - your query is showing everything from Sales AND Purchases.
These articles might help you understand
Visual Representation of SQL Joins[^]
Joining Tables in SQL[^]

What you probably want is a UNION or something like this
SQL
select salesDB.id, 'SALE' as tranType, sales
from salesDB
UNION
select purchDB.id, 'PURC' as tranType, purchase
from purchDB
which should give
1 SALE 5000
1 PURC 10000
1 PURC 15000
 
Share this answer
 
Comments
vijay_bale 11-Feb-19 4:04am    
@ CHILL60, that above code also giving wrong results
four records from sales what i am having (all are different ids means items) and 2 records from that purchase what i am having. Here Were clause I have to mention in which place ?
OriginalGriff 11-Feb-19 4:12am    
So show us exactly what is in the Sales and Purchase tables - complete rows, complete with column names - or we don't know what you are starting with!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900