Monday, March 12, 2012

query to get the data of three tables?

Hi:
I have three tables in my db. One is clients, other calls, and other visits.
I want to get all the calls and visits of all clients in my db every row in each table separate in one row in the results table.
How can I do it?
Use the SQL JOIN functionality:
http://www.w3schools.com/sql/sql_join.asp
|||You can do this a couple of ways,
Let's say the tables look like this:
table calls
clientid,
calldate
table visits
clientid,
visitdate
table client
clientid,
clientname
You can get all of the data in one row by joining the three tables together, or you can get all of the calls data and all of the visits data (in separate rows) by using a union query.
First inner joining:
select c.ClientName, call.CallDate,visit.VisitDate from calls call inner join clients c on c.clientid = call.clientid inner join visits visit on c.clientid = visit.clientid
now for the union
select c.clientname, 'call' as type, call.calldate from clients c inner join calls call on c.clientid = call.clientid
union
select c.clientname 'visit' as type, visit.visitdate from clients c inner join visits visit on c.clientid = visit.clientid
Is this what you were looking for?
Hope this helps,
|||thanks so much.
The second one is exactly what i was looking for.
Thanks again

No comments:

Post a Comment