Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select record three tables

Author  Topic 

amirs
Constraint Violating Yak Guru

260 Posts

Posted - 2009-01-19 : 02:42:29
Dear Members
please help me to following select query

i give three table table1 ,table2,table3
there are three colomn name,age,date in three table

i have select all record in table2 of todays date and select record of table1 to maching of table2 of todays date and select record in table3 to maching of table2 but previous date of table3 in desc order

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-19 : 03:14:02
use UNION ALL or UNION

SELECT *
FROM
(
SELECT name,age,date
FROM table2
WHERE date>=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
AND date<DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)
UNION ALL
SELECT name,age,date
FROM table1 t
WHERE date>=DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)
AND date<DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)
AND EXISTS(SELECT 1 FROM table2 where date>=(dd,datediff(dd,0,t.date),0) and date<(dd,datediff(dd,0,t.date),1) AND name=t.name)
UNION ALL
SELECT name,age,date
FROM table3 t
WHERE EXISTS(SELECT 1 FROM table2 where date>=dateadd(dd,datediff(dd,0,t.date),0)-1 AND date<dateadd(dd,datediff(dd,0,t.date),0) AND name=t.name)
)t
Go to Top of Page
   

- Advertisement -