'how to access data from two tables without foreign key in sql server 2005
here i have two tables like employee and one mobileoutbox table but these two tables are not connected to each other but i want to access data from two table i want to match data from two table and generate report .how to access data from two table without foreign key? tables structure :
employee table mobileoutbox table
empid srno
empname mobileno
empphoneno textmsg
empaddress texttype
Solution 1:[1]
SELECT e.empid, e.empname, e.empphoneno, e.empaddress, m.srno, m.mobileno, m.textmsg, m.texttype
FROM employee e, mobileoutbox m
WHERE e.empphoneno = m.mobileno
Solution 2:[2]
SELECT em.*, mo.*
FROM employee em, mobileoutbox mo
WHERE em.empphoneno = mo.mobileno
Tell me if anything is uncertain.
Oh and, if you need to "match" data, then there MUST be a foreign key, some sort of overlapping information between the two tables, i hope you understand that much. I used the phone number in my example as the foreign key, i don't know if that's the reality of the matter.
Solution 3:[3]
Union can do the trick in this case. Just one constraint is you should define the proper columns (one or more) as shown:
Select bt.PlayerName from dbo.Batting bt where matchNo = 34
Union
Select bo.PlayerName from dbo.Bowling bo where matchNo = 34
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gjorgji Tashkovski |
| Solution 2 | |
| Solution 3 | KushalSeth |
