I’m having some problems with join query. What I want is select rows of two tables that have some similar states, others no. And I have a state-equivalence view
Table Sales:
ID ..... StateID
1 1
2 1
3 6
Table Orders
ID ..... StateID
11 2
12 2
15 3
Table StatesEquivalence
ID SalesState OrdersState StateName
1 1 2 Attended
2 2 3 Declined
I’ve made a union of both tables(Sales and Orders) and I want a column with the EquivalentStateID and the StateName by doing something like this:
SELECT sales.ID as ID,equivalence.ID as State,equivalence.StateName
FROM Sales
INNER JOIN StatesEquivalence as equivalence
ON sales.StateID = equivalence.SalesState
WHERE sales.ID = 1
UNION
SELECT orders.ID as ID,equivalence.ID as State,equivalence.StateName
FROM Orders as orders
INNER JOIN StatesEquivalence as equivalence
ON orders.StateID = equivalence.OrdersState
Somehow I am obtaining wrong information on the equivalentID
ID State StateName
1 2 Attended
2 2 Attended
11 2 Attended
...
I don’t know what happend.. because the statename is correct, but the StateID is showing wrong information
The table might show this:
ID State StateName
1 1 Attended
2 1 Attended
11 1 Attended
...
I don’t think you are showing us the real problem code. I did the following:
Result:
So, it works. Can you try the above code?