Friday, December 23, 2011

selecting multiple records from two tables

selecting multiple records from two tables

SELECT MIN(TableName) as TableName, ID, COL1, COL2, COL3 ...
FROM
(
SELECT 'Table A' as TableName, A.ID, A.COL1, A.COL2, A.COL3, ...
FROM A
UNION ALL
SELECT 'Table B' as TableName, B.ID, B.COL1, B.COl2, B.COL3, ...
FROM B
) tmp
GROUP BY ID, COL1, COL2, COL3 ...
HAVING COUNT(*) = 1
ORDER BY ID

union select two tables records

union select two tables records by anil nain sonipat haryana
select*from FAMILY whereEXISTS
(select*
from FAMILY_old
where FAMILY_old.Family_Id = FAMILY.Family_Id and FAMILY_old.Family_Score<=7
)and Edit_ind isnull
//////////////////////////////////////////////////////
update family set edit_ind=null where not EXISTS
(select *
from FAMILY_old
where FAMILY_old.Family_Id = FAMILY.Family_Id and FAMILY_old.Family_Score<=7
) and Edit_ind=1
/////////////////////jyada records dhundne ke liye////////
select * from FAMILY where not EXISTS
(select *
from FAMILY_old
where FAMILY_old.Family_Id = FAMILY.Family_Id and FAMILY_old.Family_Score<=7
) and Edit_ind=1

select FAMILY.family_id, FAMILY.form_no,FAMILY.house_number,FAMILY.Name_Of_Head, villages.Village_Name from FAMILY inner join villages on villages.Village_Code=family.Village_Code where EXISTS
(select *
from FAMILY_old
where FAMILY_old.Family_Id = FAMILY.Family_Id and FAMILY_old.Family_Score<=7
) and Edit_ind is null

union select two tables records by anil nain sonipat haryana